In one blog post I wrote about how we can make HTTP requests and receive response in Java. That post was only about sending GET request. This time we will learn how to make POST (submitting form etc) request in Java.
This post assumes that you are familiar with HttpURLConnection found in java.net package. If you are not please read this blog post
http://random-stuff-mine.blogspot.com/2013/06/http-request-and-response-java.html
HTTP POST request
Lets say you entered username/password in a website and pressed "Login" button. Most probably the web browser makes a HTTP POST request containing your username/password. A simple POST request would look like this
POST /path/to/script HTTP/1.1
User-Agent: RemzyHttpBot
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
username=rameez&password=testIn the above request you see that username and password are sent inside the request body and content-type of request is set to application/x-www-form-urlencoded. This content type tells the webserver that POST parameters are in url encoded form. Now lets mimic this request with Java code
POSTing a form using java code
First we will create HttpURLConnection and set its parameters for POST request.
URL postUrl=new URL("http://localhost:8080/testpost.php");After creating HttpURLConnection we will open its output stream to write our POST data (username/password) in url encoded form
HttpURLConnection conn=(HttpURLConnection)postUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent","RemzyHttpBot");
conn.setRequestProperty("Content-Length",lengthOfPostBody);
conn.setDoInput(true);
conn.setDoOutput(true); //we are POSTing so we will write data to outputstream
OutputStream os=conn.getOutputStream();In above code you see the reference to URLUTF8Encoder class. This class is provided by w3.org as a helper class to convert string data into url safe form. You can get this class from here.
String postBody="username="+URLUTF8Encoder.encode("rameez");
postBody+="&password="+URLUTF8Encoder.encode("test");
os.write(postBody.getBytes()); //write our post data to outputstream
Now our POST data is sent to OutputStream of HttpURLConnection. Its time to read the response returned from server. For that we will use InputStream of HttpURLConnection
InputStream is=conn.getInputStream();So this is how you make HTTP POST request using your java code.
String connLenValue=conn.getHeaderField("Content-Length");
ByteBuffer buffer=ByteBuffer.allocate(Integer.parseInt(connLenValue));
//it will hold bytes from InputStream before pushing into buffer
byte[] tempBuff=new byte[1024];
//bread will hold number of bytes read from InputStream. -1 indicates end of response (stream)
int bread=is.read(tempBuff,0,1024);while(bread!=-1){
buffer=buffer.put(tempBuff,0,bread);
bread=is.read(tempBuff,0,1024);
}
is.close();
os.close();
conn.disconnect();
In next blog post we will see how to make a Multipart POST request which involve file uploading through java code.
Comments
Post a Comment
Share your wisdom