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

相关文章
相关标签/搜索