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涉及的代码去掉,即可以直接登录。
分享到:
相关推荐
使用HttpClient4.5实现https请求忽略SSL证书验证工具类
不会的可以在评论区留言哈,这是我自己做项目用到的。所以绝对可用!同时共享出来给到大家
httplient向https发送请求会因为证书校验而报错,该工具类提供绕开HTTPS证书校验方法,以实现访问https网站的功能
httpclient4 微信公众账号模拟登陆,网络上看到的,觉得不错上传出来,供大家一起共同分享学习。
以上自己测试是可行的,直接拿过去导入即可,直接用main
httpclient4之百度模拟登陆,回复与58同城自动登陆
httpClient自动接受https请求的所有证书代码,修改相应测试url,主函数运行即可
HttpClient4.3模拟登陆新版12306
httpclient模拟登陆,请大家快过来赶紧下载哦。
httpClient例子,支持代理(去掉注释即可用代理),已封装好util(post,get),JAR也有,打开导入工程就可以用。
httpclient.jar包及其POST方法使用例子
HttpClient配置SSL绕过https证书实例,附件中包含所需httpclient组件jar库。博客地址:http://blog.csdn.net/irokay/article/details/78801307。
javase http通讯技术 apache httpclient4.3 设置代理详解
使用httpclient和httpsurlconnection两种方法访问https网址,同时分为验证证书和不验证证书两种方法
工程里的入口类为:mnLogin.java ,由于模拟登陆的网站,使用了自签名的SSL证书,所以新增两个方法,创建HttpClient不校验服务端证书,后台通过调用登陆接口,获得返回的cookie信息,在使用登陆成功获得cookie,调取...
http远程接口调用-httpClient+跳过SSL证书校验. 里面分别有3个httpClient的工具类代码。 还有一个专门为了跳过SSL证书校验的工具类。
HttpClient之Https应用实例~ 包含: HttpClient 使用代理访问Https HttpClient 信任所有主机-对于任何证书都不做检查 HttpClient 允许所有证书的校验(包括过期证书)
此资源为新浪微博和腾讯微博的HttpClient4模拟登录的代码Sina.java为新浪的,TencentClient.java为腾讯的。
模拟登陆方正系统,在此基础上可以做开发类似课程格子之类的软件供个人或者多人之用
Jsoup+httpclient模拟登陆和抓取页面.pdf