关于HttpClient的总结

关于Httpclient的使用总结如下:
  1. (1)当HttpClient的实例不再需要时,可以使用连接管理器关闭  
  2. httpclient.getConnectionManager().shutdown();    
[java] view plain copy
  1. (2)针对HTTPs的协议的HttpClient请求必须用户和密码   
  2.  httpclient.getCredentialsProvider()   
  3.             .setCredentials(new AuthScope("localhost"443),    
  4.                 new UsernamePasswordCredentials("username""password")); 
  1. (3)如果不想获取HTTPClient返回的信息  
  2.    httpclient.abort();  
[java] view plain copy
  1. (4)httpclient传送文件的方式   
  2.         HttpClient httpclient = new DefaultHttpClient();   
  3.         HttpPost httppost = new HttpPost("http://www.apache.org");   
  4.         File file = new File(args[0]);   
  5.         InputStreamEntity reqEntity = new InputStreamEntity(   
  6.                 new FileInputStream(file), -1);   
  7.         reqEntity.setContentType("binary/octet-stream");   
  8.         reqEntity.setChunked(true);   
  9.         // It may be more appropriate to use FileEntity class in this particular   
  10.         // instance but we are using a more generic InputStreamEntity to demonstrate  
  11.         // the capability to stream out data from any arbitrary source  
  12.         //    
  13.         // FileEntity entity = new FileEntity(file, "binary/octet-stream");   
  14.         httppost.setEntity(reqEntity);   
  15.         System.out.println("executing request " + httppost.getRequestLine());   
  16.         HttpResponse response = httpclient.execute(httppost); 
  1. (5)获取Cookie的信息   
  2.         HttpClient httpclient = new DefaultHttpClient();   
  3.         // 创建一个本地Cookie存储的实例   
  4.         CookieStore cookieStore = new BasicCookieStore();   
  5.         //创建一个本地上下文信息   
  6.         HttpContext localContext = new BasicHttpContext();   
  7.         //在本地上下问中绑定一个本地存储   
  8.         localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);   
  9.         //设置请求的路径   
  10.         HttpGet httpget = new HttpGet("http://www.google.com/");    
  11.         //传递本地的http上下文给服务器   
  12.         HttpResponse response = httpclient.execute(httpget, localContext);   
  13.         //获取本地信息   
  14.         HttpEntity entity = response.getEntity();   
  15.         System.out.println(response.getStatusLine());   
  16.         if (entity != null) {   
  17.             System.out.println("Response content length: " + entity.getContentLength());   
  18.         }   
  19.         //获取cookie中的各种信息   
  20.         List<Cookie> cookies = cookieStore.getCookies();   
  21.         for (int i = 0; i < cookies.size(); i++) {   
  22.             System.out.println("Local cookie: " + cookies.get(i));   
  23.         }   
  24.         //获取消息头的信息   
  25.         Header[] headers = response.getAllHeaders();   
  26.         for (int i = 0; i<headers.length; i++) {   
  27.             System.out.println(headers[i]);   
  28.         }  
  1. (6)针对典型的SSL请求的处理   
  2.         DefaultHttpClient httpclient = new DefaultHttpClient();   
  3.         //获取默认的存储密钥类   
  4.         KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());    
  5.         //加载本地的密钥信息          
  6.         FileInputStream instream = new FileInputStream(new File("my.keystore"));    
  7.         try {   
  8.             trustStore.load(instream, "nopassword".toCharArray());   
  9.         } finally {   
  10.             instream.close();   
  11.         }   
  12.         //创建SSLSocketFactory,创建相关的Socket   
  13.         SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);   
  14.         //设置协议的类型和密钥信息,以及断开信息   
  15.         Scheme sch = new Scheme("https", socketFactory, 443);   
  16.         //在连接管理器中注册中信息   
  17.         httpclient.getConnectionManager().getSchemeRegistry().register(sch); 
  [java] view plain copy
  1. (7)设置请求的参数的几种方式   
  2. A.在请求的路径中以查询字符串格式传递参数   
  3. B.在请求的实体中添加参数   
  4.         List <NameValuePair> nvps = new ArrayList <NameValuePair>();   
  5.         nvps.add(new BasicNameValuePair("IDToken1""username"));   
  6.         nvps.add(new BasicNameValuePair("IDToken2""password"));   
  7.         httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值