1. 程式人生 > >org.apache.http.client.HttpClient get/post請求

org.apache.http.client.HttpClient get/post請求

請求步驟

1、建立httpclient 物件

2、建立 請求方式,建構函式的值為請求路徑

3、呼叫1中物件的execute() 方法,引數為 2 的物件

4、獲取請求響應資料

5、釋放連線資源

6、處理資料

一、使用org.apache.http.client.HttpClient 的get請求來實現

1、請求核心程式碼:

// 建立 httpclient 物件   
    HttpClient httpclient = new DefaultHttpClient();
    //建立請求方式,因為是get請求,所以可以在url後面連線請求引數
    HttpGet  httpget= new HttpGet("http://localhost:8080/music/user/delete.do?id=110&name=name");
    try{
    // 執行請求,獲取響應資料
    HttpResponse response = httpclient.execute(httpget);
    //  獲取請求實體,
    HttpEntity entity = response.getEntity();
    // 獲取返回例項的內容的長度
        long len= entity.getContentLength();
        // 獲取 content type
        Header  head= entity.getContentType();
        String  bodys= head.toString();
        System.out.println("內容長度"+len +"head 內容"+bodys);
     //例項流的獲取
    if(entity != null){
    InputStream input= entity.getContent();
    byte []buffer= new byte[2048];
    input.read(buffer);
    String body= new String(buffer,"utf-8");
    System.out.println("獲取資源"+body);
    JSONObject json= new JSONObject();
    json=JSONObject.fromObject(body);
    String ids= json.getString("id");
    String names=json.getString("name");
    System.out.print(ids);
    System.out.print(names);
    }
    }catch(HTTPException e){
        e.printStackTrace();
    }catch(IOException ee){
        ee.printStackTrace();
    }finally{
        httpget.releaseConnection();
    }


2、伺服器端響應請求核心程式碼:

請求完整路徑 url=http://localhost:8080/music/user/delete.do?

@RequestMapping(value="/delete.do")
public void  delete(String id,String name,HttpServletResponse response){
    System.out.println("請求到達 delete");
    String yid= id;
    String yname=name;
    System.out.println("請求引數"+yid);
    System.out.println("請求引數name:"+name);
    try {
        OutputStream output= response.getOutputStream();

        // 編碼的設定
        response.setCharacterEncoding("UTF-8");

        // 主題型別的設定
        response.setContentType("text/html");
        // 這裡傳遞一個json 格式資料
        JSONObject  json= new JSONObject();
        json.put("id", 10);
        json.put("name", "name");
        String jstring= json.toString();

         // 編碼設定
        byte [] b= jstring.getBytes("UTF-8");

         // 響應資料輸出
        output.write(b);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

二、使用org.apache.http.client.HttpClient 的post請求來實現

1、請求端核心程式碼

// 建立 httpclient 物件   這裡使用httpclient  
    HttpClient httpclient = new DefaultHttpClient();
    // 請求引數的組裝
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("id", "110110"));
    qparams.add(new BasicNameValuePair("name", "you name"));
    qparams.add(new BasicNameValuePair("age", "10"));
    qparams.add(new BasicNameValuePair("sex", "man"));
    try{

     // UrlEncodedFormEntity 例項將會使用URL編碼來編碼引數

     UrlEncodedFormEntity  entity = new UrlEncodedFormEntity(qparams,"UTF-8");
    HttpPost httppost= new HttpPost("http://localhost:8080/music/user/delete.do");
    // 請求引數的設定
    httppost.setEntity(entity);
    // 請求執行,以及響應資料的獲取
    HttpResponse  response = httpclient.execute(httppost);

    // 獲取請求響應
    HttpEntity entitys = response.getEntity();

       // 響應內容的長度
    long len= entity.getContentLength();

       // 響應內容的型別
    Header  head= entity.getContentType();
    String  bodys= head.toString();
    System.out.println("內容長度:"+len +" head 內容:"+bodys);
    if(entitys != null){

        // 獲取響應資料
        InputStream input= entitys.getContent();
        byte []buffer= new byte[2048];
        input.read(buffer);

         // 編碼設定
        String body= new String(buffer,"utf-8");
        System.out.println("獲取資源"+body);
    }
    }catch(Exception e){
        e.printStackTrace();
    }

2、接收請求,響應請求核心程式碼

請求完整路徑: url=http://localhost:8080/music/user/delete.do

@RequestMapping(value="/delete.do")
public void  delete(String id,String name,String age,HttpServletResponse response){
    System.out.println("請求到達 delete");
    String yid= id;
    String yname=name;
    String ages=age;
    System.out.println("請求引數"+yid);
    System.out.println("請求引數name:"+name);
    System.out.println("請求引數age:"+ages);
    try {
        OutputStream output= response.getOutputStream();

       //響應內容編碼的設定
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        // 這裡傳遞一個json 格式資料
        JSONObject  json= new JSONObject();
        json.put("id", 10);
        json.put("name", "name");
        String jstring= json.toString();
        byte [] b= jstring.getBytes("UTF-8");
        output.write(b);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

原文連結:http://my.oschina.net/u/1454838/blog/401275