10 Kasım 2013 Pazar


Standard HttpURLConnection ve Apache HttpClient kütüphanesi kullanarak Http GET isteği nasıl yapılır onun hakkında bilgi vermek istiyorum.

1-) Standard HttpURLConnection

HttpGetRequest.java 

  /*  
   * To change this template, choose Tools | Templates  
   * and open the template in the editor.  
   */  
  package main;  
  import java.io.BufferedReader;  
  import java.io.InputStreamReader;  
  import java.net.HttpURLConnection;  
  import java.net.URL;  
  /**  
   *  
   * @author Hasan Çelik  
   */  
  public class HttpGetRequest {  
    static int responseCode;  
    static String responseMessage;
  
    public static void main(String[] args) throws Exception {
  
      URL url = new URL("http://www.blog.berkadem.com/");  
      HttpURLConnection con = (HttpURLConnection) url.openConnection();  
      con.setRequestMethod("GET");  
      responseCode=con.getResponseCode();  
      responseMessage=con.getResponseMessage();  
      BufferedReader in = new BufferedReader(new InputStreamReader(  
                    con.getInputStream()));  
      String inputLine;  
      StringBuffer sonuc = new StringBuffer(); 
 
      while ((inputLine = in.readLine()) != null)   
        //System.out.println(inputLine); //dilerseniz response sonucunu bu şekilde de alabilirsiniz  
        sonuc.append(inputLine);  
      in.close();  
      //System.out.println("Sonuc = "+sonuc); //dilerseniz response sonucunu bu şekilde de alabilirsiniz. Burada dönen response değeri xml formatında da olabilirdi.  
      System.out.println("responseCode = "+responseCode);  
      System.out.println("responseMessage = "+responseMessage);  
    }  
  } 
Sonuç 

run:  
  responseCode = 200  
  responseMessage = OK  
  BUILD SUCCESSFUL (total time: 1 second)


1-) Apache HttpClient 

  /*  
   * To change this template, choose Tools | Templates  
   * and open the template in the editor.  
   */  
  package main;  
  import java.io.BufferedReader;  
  import java.io.InputStreamReader;  
  import org.apache.http.HttpResponse;  
  import org.apache.http.client.HttpClient;  
  import org.apache.http.client.methods.HttpGet;  
  import org.apache.http.impl.client.DefaultHttpClient;  
  /**  
   *  
   * @author Hasan Çelik  
   */  
  public class HttpGetRequest2 {  
    static int responseCode;  
    static String responseMessage;
  
    public static void main(String[] args) throws Exception {  

      String url = "http://www.blog.berkadem.com/";  
      HttpClient client = new DefaultHttpClient();  
      HttpGet request = new HttpGet(url);  
      HttpResponse response = client.execute(request);  
      responseCode=response.getStatusLine().getStatusCode();  
      responseMessage=response.getStatusLine().getReasonPhrase();  
      BufferedReader in = new BufferedReader(  
          new InputStreamReader(response.getEntity().getContent()));  
      String inputLine;  
      StringBuffer sonuc = new StringBuffer();
 
      while ((inputLine = in.readLine()) != null) //System.out.println(inputLine); //dilerseniz response sonucunu bu şekilde de alabilirsiniz  
      {  
        sonuc.append(inputLine);  
      }  

      in.close();  
      //System.out.println("Sonuc = "+sonuc); //dilerseniz response sonucunu bu şekilde de alabilirsiniz. Burada dönen response değeri xml formatında da olabilirdi.  
      System.out.println("responseCode = " + responseCode);  
      System.out.println("responseMessage = " + responseMessage);  
    }  
  } 
Sonuç 

  run:  
  responseCode = 200  
  responseMessage = OK  
  BUILD SUCCESSFUL (total time: 0 seconds)  


0 yorum :

Yorum Gönder