关于HttpClient的总结

来源:互联网 发布:淘宝首页添加链接代码 编辑:程序博客网 时间:2024/04/30 04:57

关于Httpclient的使用总结如下:

  1. (1)当HttpClient的实例不再需要时,可以使用连接管理器关闭
  2. httpclient.getConnectionManager().shutdown();
[java] view plaincopyprint?
  1. (1)当HttpClient的实例不再需要时,可以使用连接管理器关闭
  2. httpclient.getConnectionManager().shutdown();
  1. (2)针对HTTPs的协议的HttpClient请求必须用户和密码
  2. httpclient.getCredentialsProvider()
  3. .setCredentials(new AuthScope("localhost",443),
  4. new UsernamePasswordCredentials("username","password"));
[java] view plaincopyprint?
  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 plaincopyprint?
  1. (3)如果不想获取HTTPClient返回的信息
  2. 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 plaincopyprint?
  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. }
[java] view plaincopyprint?
  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 plaincopyprint?
  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);
  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));
原创粉丝点击