HttpClient的使用

一、HttpClient的使用。
   1.创建一个HttpClient。
   2.创建一个HttpGet请求或HttpPost请求。
   3.HttpCLient执行GET请求或POST请求。
   4.返回响应对象HttpResponse。
   5.通过HttpResponse获得响应码、头字段、请求参数、实体等。
   6.得到上述对象后,并进行相应操作。
   7.关闭连接并且释放资源。

二、HttpClient的简单功能实现。
public class HttpClientUtil {
 
 /**
  * GET请求
  * @param user
  * @param password
  * @param url
  * @return
  */
 public static String sendGet(String user,String password,String uri){
  HttpClient client =null;
  try {
   //定义一个HttpClient
      client =new DefaultHttpClient();
   //定义一个GET请求
   String params ="user="+user+"&password="+password;
   HttpGet get=new HttpGet(uri+"?"+params);
   //添加头字段
   //get.addHeader(String name , String value);
   //客户端执行GET请求,返回服务器响应
   HttpResponse response =client.execute(get);
   //得到响应码,并得到相应的内容
   int statusCode =response.getStatusLine().getStatusCode();
   if(statusCode==200){
    //得到实体
    HttpEntity entity =response.getEntity();
    //得到实体的输入流
    InputStream is=entity.getContent();
    BufferedReader br =new BufferedReader(new InputStreamReader(is));
    String line=null;
    StringBuffer sb=new StringBuffer();
    while((line=br.readLine())!=null){
     sb.append(line).append("\n");
    }
    return sb.toString();
   }else{
    System.out.println("statusCode="+statusCode);
   }
  }catch (IOException e) {
   e.printStackTrace();
  }finally{
   if(client!=null){
    //关闭连接并释放资源
    client.getConnectionManager().shutdown();
   }
  }
 
  return null;
 }
 
 /**
  * POST请求
  * @param user
  * @param password
  * @param uri
  * @return
  */
 public static String sendPost(String user,String password,String uri){
  HttpClient client=null;
  try {
   //定义一个HttpClient
   client =new DefaultHttpClient();
   //定义一个POST请求
   HttpPost post =new HttpPost(uri);
   //添加头字段
   //post.addHeader(name, value);
   //设置请求参数
   List<NameValuePair> params =new ArrayList<NameValuePair>();
   params.add(new BasicNameValuePair("user", user));
   params.add(new BasicNameValuePair("password", password));
   HttpEntity entity =new UrlEncodedFormEntity(params, HTTP.UTF_8);
   post.setEntity(entity);
   //客户端执行POST请求,并返回响应对象
   HttpResponse response =client.execute(post);
   //得到响应码
   int statusCode =response.getStatusLine().getStatusCode();
   if(statusCode==200){
    InputStream is =response.getEntity().getContent();
    BufferedReader br =new BufferedReader(new InputStreamReader(is));
    String line=null;
    StringBuffer sb =new StringBuffer();
    while((line=br.readLine())!=null){
     sb.append(line).append("\n");
    }
    return sb.toString();
   }else{
    System.out.println("statusCode="+statusCode);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   if(client!=null){
    //关闭连接并释放资源
    client.getConnectionManager().shutdown();
   }
  }
 
 
  return null;
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值