关于HttpClient的总结

关于Httpclient的使用总结如下:

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

猜你喜欢

转载自jayyanzhang2010.iteye.com/blog/2069935
今日推荐