Java has excellent support of HTTP client side. In this blog post we will have a look at how to make HTTP request in Java and download the response , saving it in a buffer.
Java implements a simple HTTP client in a class java.net.HttpURLConnection.
So our first step is to make an instance of java.net.HttpURLConnection. To do this we need java.net.URL class. We will use its constructor which takes url in a form of string as a parameter and then call its openConnection method casting the returned object to HttpURLConnection.
Here is how it is done
We can modify some properties of HttpURLConnection like
1) HTTP Request method
2) If we want to send request body or not.
3) Modify/Add any headers in HTTP request.
etc etc.
I have saved their explanation for a separate blog post where I will show you how you can POST HTTP data or upload a file etc. And we will also write a very simple HTTP download manager.
But for now lets keep it to default settings. Request method is GET , no body with request only headers.
Here is how we will get InputStream.
First lets see how many bytes of data server has returned using HTTP's Content-Length header.
Java implements a simple HTTP client in a class java.net.HttpURLConnection.
So our first step is to make an instance of java.net.HttpURLConnection. To do this we need java.net.URL class. We will use its constructor which takes url in a form of string as a parameter and then call its openConnection method casting the returned object to HttpURLConnection.
Here is how it is done
String strUrl="http://google.com";We now have HttpURLConnection object to use. Please note right now no request has been sent to the server (in our case google.com). To complete the process of sending actual request and start getting response we have to use getInputStream method of HttpURLConnection class. Actually getInputStream method is in URLConnection class which is inherited by HttpURLConnection.
URL url=new URL(strUrl);
HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
We can modify some properties of HttpURLConnection like
1) HTTP Request method
2) If we want to send request body or not.
3) Modify/Add any headers in HTTP request.
etc etc.
I have saved their explanation for a separate blog post where I will show you how you can POST HTTP data or upload a file etc. And we will also write a very simple HTTP download manager.
But for now lets keep it to default settings. Request method is GET , no body with request only headers.
Here is how we will get InputStream.
InputStream iStream=httpConn.getInputStream();Now if no error has occured, we have InputStream of the underlying connection and we can read the response sent by Webserver using this InputStream.
First lets see how many bytes of data server has returned using HTTP's Content-Length header.
String connLenValue=httpConn.getHeaderField("Content-Length");We will be storing our response in buffer. ByteBuffer class will come handy here. Before reading response first create ByteBuffer object using its static method allocate which takes capacity of type int as a parameter.
ByteBuffer buffer=ByteBuffer.allocate(Integer.parseInt(connLenValue));We have everything in place, so lets start reading the response from InputStream and push it into the buffer. We will use put method of ByteBuffer class which takes byte array, offset and length as parameter.
//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=iStream.read(tempBuff,0,1024);while(bread!=-1){
buffer=buffer.put(tempBuff,0,bread);
bread=iStream.read(tempBuff,0,1024);
}
httpConn.disconnect();
Comments
Post a Comment
Share your wisdom