我是靠谱客的博主 超帅棒棒糖,最近开发中收集的这篇文章主要介绍关于HttpClient的总结,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

关于Httpclient的使用总结如下:

  1. (1)当HttpClient的实例不再需要时,可以使用连接管理器关闭
  2. httpclient.getConnectionManager().shutdown();
[java] view plain copy print ?
  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"));
[java] view plain copy print ?
  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();
[java] view plain copy print ?
  1. (3)如果不想获取HTTPClient返回的信息
  2. httpclient.abort();
(3)如果不想获取HTTPClient返回的信息
   httpclient.abort();
  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);
[java] view plain copy print ?
  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);
   
(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. }
[java] view plain copy print ?
  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);
[java] view plain copy print ?
  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));

最后

以上就是超帅棒棒糖为你收集整理的关于HttpClient的总结的全部内容,希望文章能够帮你解决关于HttpClient的总结所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部