HttpClient不验证证书通过代理模拟登陆HTTPS的例子_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > HttpClient不验证证书通过代理模拟登陆HTTPS的例子

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

 2014/6/24 18:32:11  NealCai  程序员俱乐部  我要评论(0)
  • 摘要:packagecom;publicclassHttpClientLoginProxy{publicstaticvoidmain(String[]args)throwsException{CookieStorecookieStore=newBasicCookieStore();HttpClientContextcontext=HttpClientContext.create();context.setCookieStore(cookieStore)
  • 标签:例子 client 代理 HTTP
class="java" name="code">package com;

public class HttpClientLoginProxy {
public static void main(String[] args) throws Exception{
		
	
		CookieStore cookieStore = new BasicCookieStore();
	
		HttpClientContext context = HttpClientContext.create();
		context.setCookieStore(cookieStore);
		
		RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
		
		SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();

		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		//设置https验证的代理,8002为代理Port
		credsProvider.setCredentials(new AuthScope("ProxyIp", 8002),new UsernamePasswordCredentials("yourProxyId", "yourProxyPwd"));

		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,new String[] { "TLSv1" },null,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig).setDefaultCredentialsProvider(credsProvider).setDefaultCookieStore(cookieStore).setSSLSocketFactory(sslsf).build();
		try {
			
			HttpHost httpHost = new HttpHost("yourHostIp", 443, "https");
			//设置代理,8002为代理Port
			HttpHost proxy = new HttpHost("ProxyIp", 8002);
			RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
			//Login的URL
			HttpPost httppost = new HttpPost("/LOGIN");
			httppost.setConfig(config);
			//表单填写
			List<NameValuePair> formparams = new ArrayList<NameValuePair>();
			formparams.add(new BasicNameValuePair("admin", "admin"));
			formparams.add(new BasicNameValuePair("password", "password"));
			formparams.add(new BasicNameValuePair("destination", "/index.html"));
			
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
			httppost.setEntity(entity);

			//System.out.println("Executing request " + httppost.getRequestLine() 	+ " to " + httppost + " via " + proxy);

			ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
				public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
					//status可以手动登陆后用firebug或者fiddler抓取返回
					if (status >= 200 && status < 303) {
						HttpEntity entity = response.getEntity();
						return entity != null ? EntityUtils.toString(entity) : null;
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}
			};

			//登录
			System.out.println("===========执行登录=============");
			String response = httpclient.execute(httpHost,httppost,responseHandler,context);
            System.out.println(response);
            httppost.releaseConnection();
            System.out.println("===========访问第二个页面===========");

            //访问登陆后的第二个页面,并打印出来,这边要注意第二个页面是Post还是Get方式提交表单,如果是post请用HttpPost
            HttpGet httpgetConn = new HttpGet("yourNextPageUrl");
            httpgetConn.setConfig(config);
            String responseConn = httpclient.execute(httpHost,httpgetConn,responseHandler);

            System.out.println(responseConn);
          
            
      
		} finally {
			
            httpclient.close();
		}

	}
}


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

发表评论
用户名: 匿名