• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get vs Post

 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain the real differences between get and a post.
a. This is what I did.
II wrote a program using Get and did somethoing like :
PrintWriter out = new PrintWriter();
out.print("GET " +url+ "HTTP/r/r/n");
In this case the program returns me the html in the page . Now when I do:
PrintWriter out = new PrintWriter();
out.print("POST " +url+ "HTTP/r/r/n");
the program works but it does not return anything.

Can anyone explain how get and post differ and how they work internally?
Thanks.
Chiran
 
Ricardo Cortes
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us your program that you wrote? (Remember to use the <CODE> attributes)
 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is:
Do not woryy about the SSL code, Just look at the GET vs POST. GEt always retyurns something but POST doesn't
<CODE>
public void run() throws Exception {
String thisLine;
// Get a SocketFactory object for creating SSL sockets
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
// Create a secure socket connected to the HTTPS port (port 443) of a server
SSLSocket sslsock = (SSLSocket) factory.createSocket("iweb.verizon.com", 443);
//SSLSocket sslsock = (SSLSocket) factory.createSocket("java.sun.com", 443);
// Get the certificate presented by the web server. This may throw an
// exception if the server didn't supply a certificate. Look at the
// issuer of the certificate and decide if it is trusted.
SSLSession session = sslsock.getSession();
X509Certificate cert = (X509Certificate)session.getPeerCertificateChain()[0];
String issuer = cert.getIssuerDN().getName();
// Assuming we trust the certificate, we now use the socket just like a normal
// java.net.Socket object. So send a HTTP request and read the response
PrintWriter out = new PrintWriter(sslsock.getOutputStream());
//out.print("GET " + "/j2se/1.4/docs/api/java/lang/IllegalArgumentException.html" + " HTTP/1.0\r\n");
out.write("POST " + "/eweb/secure/web/jsp/index.jsp" + " HTTP/1.0\r\n\r\n");
out.flush();

// Next, read the server's response and print it to the console.
BufferedReader in =
new BufferedReader(new InputStreamReader(sslsock.getInputStream()));
String line;
while((line = in.readLine()) != null) System.out.println(line);
out.close();
// Finally, close the socket.
sslsock.close();
</CODE>

}
 
Kyle Tang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, if your program sends a HTTP Get request, and the request url is a html page, then you will see that html page. if the url is a servlet, or jsp, you will see what the doGet() responses. if the doGet writes something, you see it, if it writes nothing, you see nothing. (you got only Http status code and Http headers)
if your program sends a HTTP Post request, but the url(in your program it is a jsp page) does not generate any response, then you got nothing.
both Get and Post could generate responses, binary or text, but it could just response with Http status code and Http headers, with no content returned. Depend on what the requested url does.
I am not sure if I am answering the stuff you want to know.
 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This URL when using GET returns the html. However when using POST it doesn't return anything. That's what I cannot understand.
 
Kyle Tang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a static resource can't handle a POST request.
web server receives the HTTP request.
If the requested url is a static resource,
if it is a GET, web server will return that resource.
if it is a POST, what will be returned depends on the web server. it may be status_code 405, for example.
If the requested url is a dynamic resource, say a Servlet or jsp, web server will let the dynamic resouce handle the request. No matter whether it is GET, POST, PUT, DELETE and other HTTP requests.
POST means "I give you this posted data, you process it for me, and return me the result". static resources can't process the POST request.
GET means, "hey web server, give me that url".
 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, I got it Kyle great explanation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic