View RSS Feed

Java Socket

How to send HTTP request to a URL

Rating: 2 votes, 1.00 average.
by , 12-09-2011 at 07:28 AM (23732 Views)
Sometimes an application needs to communicate to a web server to send/receive data through HTTP. For example, connecting to a financial service to get stock price, posting user data to server to save in database, or checking for a new version of an application if available.

This article will guide you to write a generic utility method that can connect to a specified URL with either HTTP’s GET/POST method, and attaching parameters to the request. The method will return server’s response in form of an array of Strings. Your application then parses the result to decide what to do next.

The Java API for URL

In order to implement the code, we plan to use some classes from the two packages: java.net and java.io.

  • The main class in java.net package is the URLConnection class that initiates a connection to the server, opens the connection’s output stream to send POST data, opens the connection’s input stream to receive server’s response.
  • The main classes in java.io package are the InputStreamReader class and OutputStreamReader class. The former class wraps the connection’s input stream and provide convenient methods to read the response. The latter class wraps the connection’s output stream and provides convenient methods to attach POST data along with the request.


Writing the utility

The code of the utility is as following:


Java Code: The implementation of HttpRequestUtility class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HttpRequestUtility {
   
    /**
     * Makes a HTTP request to a URL and receive response
     * @param requestUrl the URL address
     * @param method Indicates the request method, "POST" or "GET"
     * @param params a map of parameters send along with the request
     * @return An array of String containing text lines in response
     * @throws IOException
     */
    public static String[] sendHttpRequest(String requestUrl, String method,
            Map<String, String> params) throws IOException {
        List<String> response = new ArrayList<String>();
       
       StringBuffer requestParams = new StringBuffer();
       
       if (params != null && params.size() > 0) {
           Iterator<String> paramIterator = params.keySet().iterator();
           while (paramIterator.hasNext()) {
               String key = paramIterator.next();
               String value = params.get(key);
               requestParams.append(URLEncoder.encode(key, "UTF-8"));
               requestParams.append("=").append(URLEncoder.encode(value, "UTF-8"));
               requestParams.append("&");
           }
       }
       
       URL url = new URL(requestUrl);
       URLConnection urlConn = url.openConnection();
       urlConn.setUseCaches(false);
       
       // the request will return a response
       urlConn.setDoInput(true);
       
       if ("POST".equals(method)) {
           // set request method to POST
           urlConn.setDoOutput(true);
       } else {
           // set request method to GET
           urlConn.setDoOutput(false);
       }
       
       if ("POST".equals(method) && params != null && params.size() > 0) {
           OutputStreamWriter writer = new OutputStreamWriter(urlConn.getOutputStream());
           writer.write(requestParams.toString());
           writer.flush();   
       }
       
       // reads response, store line by line in an array of Strings
       BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            response.add(line);
        }
       
        reader.close();
       
       
        return (String[]) response.toArray(new String[0]);
    }
}


As you can see, we have written a static method, sendHttpRequest() which accepts three parameters:

  • requestUrl: The URL address that needs to be connected. If you send parameters by GET, you should append them before the ? sign at the end of the URL.
  • method: should be either POST or GET.
  • params: it is only used if the method is “POST”. A map that stores parameters in form of key-value pairs. The above code iterates through this map to build a string of parameters in the form of: name1=value1&name2=value2… and the parameter’s name and value should be encoded, to ensure that some special characters like +, -, &, =, space, etc… are escaped and be understood as normal characters.


If the request method is POST, the code will write parameters to the request output stream. Server’s response will be read into an array of String with each item is for a text line in the response, via the request input stream. The method finally returns the array of Strings if connection is made successfully and server returns a response. Otherwise, an IOException is thrown and your application has to catch this exception.

Server side handling for HTTP POST/GET

You can use any server side script (PHP, ASP, Java…) to handle the request, since the request is HTTP. If you implement the server side by a Java servlet, remember to override either servlet’s doGet() and doPost() methods.

  • The doGet() method will be invoked if the request’s method is GET.
  • The doPost() method will be invoked if the method of the request is POST.




The following code snippet illustrates a skeleton of a servlet that overrides both doGet() and doPost() methods:


Java Code: A Java serlvet that handles HTTP GET and POST methods
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UploadServlet extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // handles GET request
        String paramValue = request.getParameter("paramName");
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String filePath = "D:/Temp/Upload/photo.png”

        // this is to handle POST request
        // reads bytes sent along with the POST request
        BufferedInputStream bis = new BufferedInputStream(request
                .getInputStream());

        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(filePath));

        byte[] bytesIn = new byte[4096];
        int read = 0;

        while ((read = bis.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }

        bos.close();
        bis.close();
    }
}


The doPost() method opens an input stream from the request, read up all bytes sent from client then save them into a file on the server. The above code is commonly used to handle file upload from a desktop application.

Writing the test program

We write a simple test program that demonstrates the use of above utility to connect to Google home page (using GET method) and try to login to Gmail (using POST method). The code looks like the following:


Java Code: A simple program that uses the HttpRequestUtility class's method
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import hainasoft.codelib.net.HttpRequestUtility;

public class TestHttpApp {
    public static void main(String[] args) {
        String requestUrl = "http://www.google.com";
        String method = "GET";
        Map<String, String> params = new HashMap<String, String>();
       
        try {
            String[] response = HttpRequestUtility.sendHttpRequest(requestUrl, method, params);
            if (response != null && response.length > 0) {
                System.out.println("RESPONSE FROM: " + requestUrl);
                for (String line : response) {
                    System.out.println(line);
                }
            }
           
            requestUrl = "http://accounts.google.com/ServiceLoginAuth";
            params.put("Email", "hainatu@gmail.com");
            params.put("Passwd", "abc");
           
            response = HttpRequestUtility.sendHttpRequest(requestUrl, method, params);
           
            if (response != null && response.length > 0) {
                System.out.println("RESPONSE FROM: " + requestUrl);
                for (String line : response) {
                    System.out.println(line);
                }
            }               
        } catch (IOException ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
    }

}


Because the method sendHttpRequest is static, we don’t need create an instance of the class HttpRequestUtility.

Compile and run the TestHttpApp program as a Java application.

First, the program connects to google home page (http://www.google.com) using GET method then outputs the server’s response which is actually HTML code for the page.

Second, the program connects to Gmail login page (http://accounts.google.com/ServiceLoginAuth) using POST method, passing two parameters, “Email” and “Passwd” which are expected by the server. The HTML code returned is for a page says “Moved Temporarily”, because the actual login page using HTTPS rather than HTTP. We just use that for demonstration purpose only.

Conclusion

So far we have written a utility class that provides an out-of-the-box method which connects to a URL using either POST or GET method. The utility method is generic so you can reuse it in your application, and your application is only aware of how to deal with the response.

Submit "How to send HTTP request to a URL" to Facebook Submit "How to send HTTP request to a URL" to Digg Submit "How to send HTTP request to a URL" to del.icio.us Submit "How to send HTTP request to a URL" to StumbleUpon Submit "How to send HTTP request to a URL" to Google

Tags: http, java, post, request, url Add / Edit Tags
Categories
HTTP , URL

Comments