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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值