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

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涉及的代码去掉,即可以直接登录。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值