Skip to main content

Posts

Showing posts with the label download

HTTP requests and response in Java

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 String strUrl="http://google.com"; URL url=new URL(strUrl); HttpURLConnection httpConn=(HttpURLConnection)url.openConnection(); 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 getInputStr...