HttpClient不验证证书通过代理模拟登陆HTTPS的例子

Java代码   收藏代码
  1. package com;  
  2.   
  3. public class HttpClientLoginProxy {  
  4. public static void main(String[] args) throws Exception{  
  5.           
  6.       
  7.         CookieStore cookieStore = new BasicCookieStore();  
  8.       
  9.         HttpClientContext context = HttpClientContext.create();  
  10.         context.setCookieStore(cookieStore);  
  11.           
  12.         RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();  
  13.           
  14.         SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(nullnew TrustSelfSignedStrategy()).build();  
  15.   
  16.         CredentialsProvider credsProvider = new BasicCredentialsProvider();  
  17.         //设置https验证的代理,8002为代理Port  
  18.         credsProvider.setCredentials(new AuthScope("ProxyIp"8002),new UsernamePasswordCredentials("yourProxyId""yourProxyPwd"));  
  19.   
  20.         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,new String[] { "TLSv1" },null,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
  21.         CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig).setDefaultCredentialsProvider(credsProvider).setDefaultCookieStore(cookieStore).setSSLSocketFactory(sslsf).build();  
  22.         try {  
  23.               
  24.             HttpHost httpHost = new HttpHost("yourHostIp"443"https");  
  25.             //设置代理,8002为代理Port  
  26.             HttpHost proxy = new HttpHost("ProxyIp"8002);  
  27.             RequestConfig config = RequestConfig.custom().setProxy(proxy).build();  
  28.             //Login的URL  
  29.             HttpPost httppost = new HttpPost("/LOGIN");  
  30.             httppost.setConfig(config);  
  31.             //表单填写  
  32.             List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
  33.             formparams.add(new BasicNameValuePair("admin""admin"));  
  34.             formparams.add(new BasicNameValuePair("password""password"));  
  35.             formparams.add(new BasicNameValuePair("destination""/index.html"));  
  36.               
  37.             UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);  
  38.             httppost.setEntity(entity);  
  39.   
  40.             //System.out.println("Executing request " + httppost.getRequestLine()   + " to " + httppost + " via " + proxy);  
  41.   
  42.             ResponseHandler<String> responseHandler = new ResponseHandler<String>() {  
  43.                 public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {  
  44.                     int status = response.getStatusLine().getStatusCode();  
  45.                     //status可以手动登陆后用firebug或者fiddler抓取返回  
  46.                     if (status >= 200 && status < 303) {  
  47.                         HttpEntity entity = response.getEntity();  
  48.                         return entity != null ? EntityUtils.toString(entity) : null;  
  49.                     } else {  
  50.                         throw new ClientProtocolException("Unexpected response status: " + status);  
  51.                     }  
  52.                 }  
  53.             };  
  54.   
  55.             //登录  
  56.             System.out.println("===========执行登录=============");  
  57.             String response = httpclient.execute(httpHost,httppost,responseHandler,context);  
  58.             System.out.println(response);  
  59.             httppost.releaseConnection();  
  60.             System.out.println("===========访问第二个页面===========");  
  61.   
  62.             //访问登陆后的第二个页面,并打印出来,这边要注意第二个页面是Post还是Get方式提交表单,如果是post请用HttpPost  
  63.             HttpGet httpgetConn = new HttpGet("yourNextPageUrl");  
  64.             httpgetConn.setConfig(config);  
  65.             String responseConn = httpclient.execute(httpHost,httpgetConn,responseHandler);  
  66.   
  67.             System.out.println(responseConn);  
  68.             
  69.               
  70.         
  71.         } finally {  
  72.               
  73.             httpclient.close();  
  74.         }  
  75.   
  76.     }  
  77. }  


  主要需要注意的是代理的配置以及表单域参数还有提交方式。 
  可以手动登陆后用firebug或者fiddler抓取返回的status以及表单的元素。 
  这个例子是可以不下载证书登录https的,如果想要以下载证书的方式,可以用比如说chrome下载了https的证书然后加载到jdk中,具体可以网上查下,但是如果网站更新了证书,模拟登陆也需要重新导入证书。 
  如果不需要使用代理,可以将proxy涉及的代码去掉,即可以直接登录。

猜你喜欢

转载自blog.csdn.net/u011402896/article/details/80016457