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

請求步驟html

一、建立httpclient 對象java

二、建立 請求方式,構造函數的值爲請求路徑apache

三、調用1中對象的execute() 方法,參數爲 2 的對象json

四、獲取請求響應數據服務器

五、釋放鏈接資源app

六、處理數據函數

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

一、請求核心代碼:編碼

  // 建立 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();
    }url

二、服務器端響應請求核心代碼:

請求完整路徑 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();
    }
}

2、使用org.apache.http.client.HttpClient 的get請求來實現,本身拼湊url 以及多個請求數據

一、請求發送核心代碼

  // 建立 httpclient 對象   這裏使用httpclient  
    HttpClient httpclient = new DefaultHttpClient();
    // 建立請求方法對象
    HttpGet httpget = new HttpGet();
    try{
    //請求參數對象的建立以及參數的設置
    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"));
    // url的拼裝,以及請求參數編碼的設置
    java.net.URI uri = URIUtils.createURI("http", "localhost:8080/music/user", -1, "/delete.do",
            URLEncodedUtils.format(qparams, "UTF-8"), null);
     // 給請求路徑設置值,據訪問路徑
    httpget.setURI(uri);
    System.out.println(httpget.getURI());
    // 執行請求,獲取響應數據
    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();
    }catch(Exception eee){
        eee.printStackTrace();
    }finally{
        httpget.releaseConnection();
    }


二、接收請求,響應請求核心代碼

請求完整路徑: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();
    }
}


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

一、請求端核心代碼

// 建立 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();
    }

二、接收請求,響應請求核心代碼

請求完整路徑: 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();    }}

相關文章
相關標籤/搜索