HttpClient不驗證證書通過代理模擬登陸HTTPS的例子
阿新 • • 發佈:2019-02-15
Java程式碼
主要需要注意的是代理的配置以及表單域引數還有提交方式。
可以手動登陸後用firebug或者fiddler抓取返回的status以及表單的元素。
這個例子是可以不下載證書登入https的,如果想要以下載證書的方式,可以用比如說chrome下載了https的證書然後載入到jdk中,具體可以網上查下,但是如果網站更新了證書,模擬登陸也需要重新匯入證書。
如果不需要使用代理,可以將proxy涉及的程式碼去掉,即可以直接登入。
- 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"
- 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涉及的程式碼去掉,即可以直接登入。