package com.kuailezhuan;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by edison on 2018/5/5.
*/
public class Klz {
private String indexURL = "http://www.lezhuan.com/";
private String act = "login";
private String tbUserAccount = "";
private String tbUserPwd = "";
boolean daili = false;
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//CloseableHttpClient httpClient = httpClientBuilder.build();
CloseableHttpClient httpClient = createSSLClientDefault();
private HttpHost proxy = new HttpHost("127.0.0.1",8888,"http");
private RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
private String useage = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
private RequestConfig configtime=RequestConfig.custom().setCircularRedirectsAllowed(true).setSocketTimeout(10000).setConnectTimeout(10000).build();
public Klz() {
}
public Klz(String tbUserAccount, String tbUserPwd) {
this.tbUserAccount = tbUserAccount;
this.tbUserPwd = tbUserPwd;
}
// client工具函数,信任对方(https)所有证书
private CloseableHttpClient createSSLClientDefault(){
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有证书
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
return HttpClients.custom().setSSLSocketFactory(sslFactory).build();
} catch (Exception e) {
}
return HttpClients.createDefault();
}
public String getPageHtml(String url) {
String html="";
HttpGet httpget = new HttpGet(url);
httpget.addHeader("User-Agent", useage);
httpget.setConfig(configtime);
try {
CloseableHttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity, "utf-8");
httpget.releaseConnection();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return html;
}
//获取token
public String getToken() throws ClientProtocolException, IOException{
HttpGet get = new HttpGet(indexURL);
get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36");
get.setHeader("Accept","*/*");
get.setHeader("Accept-Encoding","gzip,deflate");
get.setHeader("Accept-Language","zh-CN,zh;q=0.9");
get.setHeader("Origin",indexURL);
get.setHeader("Referer",indexURL);
CloseableHttpResponse response = httpClient.execute(get);
String responseHtml = EntityUtils.toString(response.getEntity());
String tokenStr = responseHtml.split("<input id=\"token\" type=\"hidden\" name=\"token\" value=\"")[1]
.split("\"/>")[0];
String token = tokenStr.substring(0, tokenStr.indexOf("/")).trim();
return token.substring(0,token.length() - 1);
}
public void login() throws IOException {
List<NameValuePair> para = new ArrayList<NameValuePair>();
Map<String, String> header = new HashMap<String, String>();
header.put("Content-Type", "application/x-www-form-urlencoded");
header.put("Referer",indexURL);
header.put("User-Agent", useage);
header.put("X-Requested-With", "XMLHttpRequest");
header.put("Host", "www.lezhuan.com");
header.put("Origin", indexURL);
para.add(new BasicNameValuePair("tbUserAccount", tbUserAccount));
para.add(new BasicNameValuePair("tbUserPwd", tbUserPwd));
para.add(new BasicNameValuePair("token", getToken()));
para.add(new BasicNameValuePair("act", act));
para.add(new BasicNameValuePair("key", String.valueOf(Math.random())));
HttpPost httppost = new HttpPost("http://www.lezhuan.com/ajax.php");
for (String string : header.keySet()) {
httppost.addHeader(string, header.get(string));
}
if (daili) {
httppost.setConfig(config);
}
httppost.setEntity(new UrlEncodedFormEntity(para,"utf-8"));
CloseableHttpResponse res = httpClient.execute(httppost);
int statuts_code = res.getStatusLine().getStatusCode();
System.out.println(statuts_code);
System.out.println(EntityUtils.toString(res.getEntity(),"utf-8"));
httppost.releaseConnection();
}
public HttpResponse login2() throws IOException {
List<NameValuePair> para = new ArrayList<NameValuePair>();
Map<String, String> header = new HashMap<String, String>();
header.put("Content-Type", "application/x-www-form-urlencoded");
header.put("Referer",indexURL);
header.put("User-Agent", useage);
header.put("X-Requested-With", "XMLHttpRequest");
header.put("Host", "www.lezhuan.com");
header.put("Origin", indexURL);
para.add(new BasicNameValuePair("tbUserAccount", tbUserAccount));
para.add(new BasicNameValuePair("tbUserPwd", tbUserPwd));
para.add(new BasicNameValuePair("token", getToken()));
para.add(new BasicNameValuePair("act", act));
para.add(new BasicNameValuePair("key", String.valueOf(Math.random())));
HttpPost httppost = new HttpPost("http://www.lezhuan.com/ajax.php");
for (String string : header.keySet()) {
httppost.addHeader(string, header.get(string));
}
if (daili) {
httppost.setConfig(config);
}
httppost.setEntity(new UrlEncodedFormEntity(para,"utf-8"));
HttpResponse res = httpClient.execute(httppost);
return res;
}
//根据cookie信息,实现自动签到
public void sigin(HttpResponse httpResponse) throws ClientProtocolException, IOException{
HttpPost httppost = new HttpPost("http://www.lezhuan.com/ajax.php");
Header[] headers = httpResponse.getHeaders("Set-Cookie");
for(int i =0 ;i<headers.length;i++){
httppost.addHeader(headers[i]);
}
Map<String, String> header = new HashMap<String, String>();
header.put("Content-Type", "application/x-www-form-urlencoded");
header.put("Referer",indexURL);
header.put("User-Agent", useage);
header.put("X-Requested-With", "XMLHttpRequest");
header.put("Host", "www.lezhuan.com");
header.put("Origin", indexURL);
for (String string : header.keySet()) {
httppost.addHeader(string, header.get(string));
}
List<NameValuePair> para = new ArrayList<NameValuePair>();
para.add(new BasicNameValuePair("act", "signin"));
para.add(new BasicNameValuePair("key", String.valueOf(Math.random())));
httppost.setEntity(new UrlEncodedFormEntity(para,"utf-8"));
CloseableHttpResponse r = httpClient.execute(httppost);
int statuts_code = r.getStatusLine().getStatusCode();
System.out.println(statuts_code);
System.out.println(EntityUtils.toString(r.getEntity(),"utf-8"));
httppost.releaseConnection();
}
public static void main(String[] args) {
Klz klz = new Klz("这里填你的用户名","这里填你的密码");
try {
HttpResponse httpResponse = klz.login2();
klz.sigin(httpResponse);
//klz.login();
String html = klz.getPageHtml("http://www.lezhuan.com/");
//System.out.println(html);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}