关于HttpClient的总结

 关于Httpclient的使用总结如下:

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

  转载请标明出处 http://blog.csdn.net/shimiso 

技术交流群:66756039


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值