Thursday, January 19, 2012

HTTP GET/POST request from java

HTTP GET Requests

An HTTP GET request is all you need to retrieve basic search results from the Yahoo! Search Web Service. GET requests are easy to debug because you can paste it in your web browser and quickly see if the query has been constructed properly. Some web services require requests via POST for tasks such as image uploading or the modification of HTTP headers to set cookies. We will cover both GET and POST requests in the following examples.
The first example is a request for the keyword "umbrella" that returns 10 results:
String request = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=umbrella&results=10";
We make a simple HTTP GET request to retrieve the results:
HttpClient client = new HttpClient();

        GetMethod method = new GetMethod(request);


  // Send GET request

        int statusCode = client.executeMethod(method);
Next, we process the response from Yahoo! Web Services and print the XML response to the console:
InputStream rstream = null;

        rstream = method.getResponseBodyAsStream();

 BufferedReader br = new BufferedReader(new InputStreamReader(rstream));

    String line;

    while ((line = br.readLine()) != null) {

    System.out.println(line);

    }

    br.close();
A complete code example is available in the file YahooWebServiceGet.java

HTTP POST Requests

POST requests are used by web browsers when submitting data from HTML forms. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
For POST requests, we construct the POST data separately from the request URL and parameters.
String request = "http://api.search.yahoo.com/WebSearchService/V1/webSearch";
    HttpClient client = new HttpClient();

    PostMethod method = new PostMethod(request);

    // Add POST parameters

    method.addParameter("appid","YahooDemo");

    method.addParameter("query","umbrella");

    method.addParameter("results","10");


    // Send POST request

    int statusCode = client.executeMethod(method);

    InputStream rstream = null;

    
    // Get the response body

    rstream = method.getResponseBodyAsStream();

 
The response is the same as the first example and can be processed in the same way.
A complete code example is available in the file YahooWebServicePost.java

......................................................

  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.OutputStreamWriter;
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. import java.net.URLEncoder;
  7.  
  8. public class POSTDATA {
  9.  
  10.  
  11.     public POSTDATA() {
  12.     }
  13.     public static void main(String[] args) {
  14.         try {
  15.         // Construct data
  16.         String data = URLEncoder.encode("fName", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
  17.         data += "&" + URLEncoder.encode("lName", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
  18.  
  19.         // Send data
  20.         URL url = new URL("http://localhost/TUT/POSTDATA/post.php");
  21.         URLConnection conn = url.openConnection();
  22.         conn.setDoOutput(true);
  23.         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  24.         wr.write(data);
  25.         wr.flush();
  26.  
  27.         // Get the response
  28.         BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  29.         String line;
  30.         while ((line = rd.readLine()) != null) {
  31.             System.out.println(line);
  32.         }
  33.         wr.close();
  34.         rd.close();
  35.     } catch (Exception e) {
  36.     }
  37.     }
  38.  
  39. }

....................................................
http://www.aviransplace.com/2008/01/08/make-http-post-or-get-request-from-java/
http://developer.yahoo.com/java/howto-reqRestJava.html#get 
http://hc.apache.org/httpclient-3.x/tutorial.html
http://www.vogella.de/articles/ApacheHttpClient/article.html

No comments:

Post a Comment