目录
携程手机版国内机票数据 携程手机版国际机票数据 携程网页版国内机票数据 携程网页版国际机票数据 去哪儿网手机版机票数据 去哪儿网网页版机票数据 携程手机版机票数据添加代理 去哪儿网网页版机票数据添加代理
原因不使用代理,访问速度稍微快一些,携程就会提示验证码或者滑块。
弊端携程端应该是已经将足够多的代理ip加入了黑名单,或者蜜罐。测试使用代理的时候,有些代理返回的数据明显不正常,而且比例不低。索性本篇仅作为技术探讨,影响不大。
代理ip选择本篇使用的代理选择了青果网络的,原因是他们家有2个小时的免费测试账号。测试结果自然是好多好多ip已经被携程拉黑了,返回的都是空数据。所以如果要成功率高一些的数据,需要看看其他家的(比如阿布云?)
代码步骤 添加代理 private static final String HTTP = "http";
private static final String HTTPS = "https";
private static SSLConnectionSocketFactory sslsf = null;
private static PoolingHttpClientConnectionManager cm = null;
private static SSLContextBuilder builder = null;
static {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
try {
builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
sslsf = new SSLConnectionSocketFactory(builder.build(),
new String[] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
Registry registry = RegistryBuilder.create()
.register(HTTP, new PlainConnectionSocketFactory()).register(HTTPS, sslsf).build();
cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(200);// max connection
} catch (Exception e) {
e.printStackTrace();
}
}
使用代理定义请求方法
public void flight() throws Exception {
JSONObject param = new JSONObject();
param.put("preprdid", "");
param.put("trptpe", 1);
param.put("flag", 8);
JSONArray array = new JSONArray();
JSONObject temp = new JSONObject();
temp.put("dccode", "SHA");
temp.put("accode", "CTU");
temp.put("dtime", "2022-01-01");
array.add(temp);
param.put("searchitem", array);
param.put("subchannel", null);
param.put("tid", "{1eb59a54-aacd-42a1-a80e-ecaa137f0d1f}");
JSONObject param2 = new JSONObject();
param2.put("cid", "09031039219678951998");
param2.put("ctok", "");
param2.put("cver", "1.0");
param2.put("lang", "01");
param2.put("sid", "8888");
param2.put("syscode", "09");
param2.put("auth", null);
JSONArray array2 = new JSONArray();
JSONObject temp01 = new JSONObject();
temp01.put("name", "aid");
temp01.put("value", "66672");
array2.add(temp01);
JSONObject temp02 = new JSONObject();
temp02.put("name", "sid");
temp02.put("value", "1693366");
array2.add(temp02);
JSONObject temp03 = new JSONObject();
temp03.put("name", "protocal");
temp03.put("value", "https");
array2.add(temp03);
param2.put("extension", array2);
param.put("head", param2);
param.put("contentType", "json");
String url = "https://m.ctrip.com/restapi/soa2/14022/flightListSearch?_fxpcqlniredt=09031039219678951998";
CookieStore store = new BasicCookieStore();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("AD573B51", "8E1A3BBD9E71"));
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm)
.setConnectionManagerShared(true).setDefaultCookieStore(store)
.setDefaultCredentialsProvider(credsProvider).build();
HttpHost myProxy = new HttpHost("tunnel.qg.net", 18901);
RequestConfig requestConfig = RequestConfig.custom().setProxy(myProxy).build();
HttpPost post = new HttpPost(url);
StringEntity postingString = new StringEntity(param.toString());
post.setConfig(requestConfig);
post.setEntity(postingString);
CloseableHttpResponse response = httpclient.execute(post);
byte[] bResultXml = EntityUtils.toByteArray(response.getEntity());
String result = new String(bResultXml, "utf-8");
System.out.println(index + "\t" + result);
}
定义线程调用上述方法
public class MyThread extends Thread {
@Override
public void run() {
try {
flight();
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试
public static void main(String[] args) throws Exception {
for(int i = 0; i < 1; i++) {
index++;
MyThread t = new Test7().new MyThread();
t.start();
Thread.sleep(200);
}
}
完整Java代码
static int index = 0;
public static void main(String[] args) throws Exception {
for(int i = 0; i < 1; i++) {
index++;
MyThread t = new Test7().new MyThread();
t.start();
Thread.sleep(200);
}
}
public class MyThread extends Thread {
@Override
public void run() {
try {
flight();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void flight() throws Exception {
JSONObject param = new JSONObject();
param.put("preprdid", "");
param.put("trptpe", 1);
param.put("flag", 8);
JSONArray array = new JSONArray();
JSONObject temp = new JSONObject();
temp.put("dccode", "SHA");
temp.put("accode", "CTU");
temp.put("dtime", "2022-01-01");
array.add(temp);
param.put("searchitem", array);
param.put("subchannel", null);
param.put("tid", "{1eb59a54-aacd-42a1-a80e-ecaa137f0d1f}");
JSONObject param2 = new JSONObject();
param2.put("cid", "09031039219678951998");
param2.put("ctok", "");
param2.put("cver", "1.0");
param2.put("lang", "01");
param2.put("sid", "8888");
param2.put("syscode", "09");
param2.put("auth", null);
JSONArray array2 = new JSONArray();
JSONObject temp01 = new JSONObject();
temp01.put("name", "aid");
temp01.put("value", "66672");
array2.add(temp01);
JSONObject temp02 = new JSONObject();
temp02.put("name", "sid");
temp02.put("value", "1693366");
array2.add(temp02);
JSONObject temp03 = new JSONObject();
temp03.put("name", "protocal");
temp03.put("value", "https");
array2.add(temp03);
param2.put("extension", array2);
param.put("head", param2);
param.put("contentType", "json");
String url = "https://m.ctrip.com/restapi/soa2/14022/flightListSearch?_fxpcqlniredt=09031039219678951998";
CookieStore store = new BasicCookieStore();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("AD573B51", "8E1A3BBD9E71"));
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm)
.setConnectionManagerShared(true).setDefaultCookieStore(store)
.setDefaultCredentialsProvider(credsProvider).build();
HttpHost myProxy = new HttpHost("tunnel.qg.net", 18901);
RequestConfig requestConfig = RequestConfig.custom().setProxy(myProxy).build();
HttpPost post = new HttpPost(url);
StringEntity postingString = new StringEntity(param.toString());
post.setConfig(requestConfig);
post.setEntity(postingString);
CloseableHttpResponse response = httpclient.execute(post);
byte[] bResultXml = EntityUtils.toByteArray(response.getEntity());
String result = new String(bResultXml, "utf-8");
System.out.println(index + "\t" + result);
}
private static final String HTTP = "http";
private static final String HTTPS = "https";
private static SSLConnectionSocketFactory sslsf = null;
private static PoolingHttpClientConnectionManager cm = null;
private static SSLContextBuilder builder = null;
static {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
try {
builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
sslsf = new SSLConnectionSocketFactory(builder.build(),
new String[] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
Registry registry = RegistryBuilder.create()
.register(HTTP, new PlainConnectionSocketFactory()).register(HTTPS, sslsf).build();
cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(200);// max connection
} catch (Exception e) {
e.printStackTrace();
}
}
2022.7.7更新
经测试原来的url已经不能使用,此处补充可以使用的完整代码。其中,请求参数中多出来几个有可能是加密之后的,也可能是固定值,尚未验证,直接从抓包里面复制过来的。
代理依然使用的qg.net上面的隧道代理(每次购买后对应的地址和端口都可能变,需要根据自己订单修改。我代理里面的是免费两小时的,过期就不能用了)
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@SuppressWarnings("deprecation")
public class Test7 {
static int index = 0;
public static void main(String[] args) throws Exception {
for(int i = 0; i < 1; i++) {
index++;
MyThread t = new Test7().new MyThread();
t.start();
Thread.sleep(200);
}
}
public class MyThread extends Thread {
@Override
public void run() {
try {
flight();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void flight() throws Exception {
JSONObject param = new JSONObject();
// param.put("preprdid", "");
// param.put("trptpe", 1);
// param.put("flag", 8);
JSONArray array = new JSONArray();
JSONObject temp = new JSONObject();
temp.put("dccode", "SHA");
temp.put("accode", "CTU");
temp.put("dtime", "2022-01-01");
array.add(temp);
JSONObject param2 = new JSONObject();
param2.put("cid", "09031039219678951998");
param2.put("ctok", "");
param2.put("cver", "1.0");
param2.put("lang", "01");
param2.put("sid", "8888");
param2.put("syscode", "09");
param2.put("auth", null);
JSONArray array2 = new JSONArray();
JSONObject temp01 = new JSONObject();
temp01.put("name", "aid");
temp01.put("value", "66672");
array2.add(temp01);
JSONObject temp02 = new JSONObject();
temp02.put("name", "sid");
temp02.put("value", "1693366");
array2.add(temp02);
JSONObject temp03 = new JSONObject();
temp03.put("name", "protocal");
temp03.put("value", "https");
array2.add(temp03);
JSONObject temp0 = new JSONObject();
temp0.put("name", "Flt_SubChannel");
temp0.put("value", "");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "mystery");
temp0.put("value", "MDkwMzEwMzkyMTk2Nzg5NTE5OTgueyJzZWFyY2hQYXJhbWV0ZXIiOnsic2VhcmNo");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "sessionId");
temp0.put("value", "e903f01-dad9-406e-8add-487982917f73");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "pageName");
temp0.put("value", "FIRST");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "searchKey");
temp0.put("value", "");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "aId");
temp0.put("value", "66672");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "sId");
temp0.put("value", "1693366");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "ouId");
temp0.put("value", "");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "sourceId");
temp0.put("value", "");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "exmktID");
temp0.put("value", "");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "env");
temp0.put("value", "h5");
array2.add(temp0);
temp0 = new JSONObject();
temp0.put("name", "version");
temp0.put("value", "2022.06.16");
array2.add(temp0);
param2.put("extension", array2);
param.put("head", param2);
JSONObject data = new JSONObject();
JSONObject searchParameter = new JSONObject();
JSONArray a = new JSONArray();
JSONObject b = new JSONObject();
b.put("departureCode", "BJS");
b.put("arrivalCode", "SHA");
b.put("departureDate", "2022-07-08");
a.add(b);
searchParameter.put("searchFlightItems", a);
searchParameter.put("tripType", "ONE_WAY");
searchParameter.put("regionType", "DOMESTIC");
JSONObject passengers = new JSONObject();
passengers.put("ADULT", "1");
passengers.put("CHILD", "0");
passengers.put("INFANT", "0");
searchParameter.put("passengers", passengers);
data.put("searchParameter", searchParameter);
param.put("data", data.toString());
String url = "https://m.ctrip.com/restapi/soa2/14488/flightList?subEnv=fat128";
CookieStore store = new BasicCookieStore();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("E9B37D77", "5CFF34EF422C"));
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm)
.setConnectionManagerShared(true).setDefaultCookieStore(store)
.setDefaultCredentialsProvider(credsProvider).build();
HttpHost myProxy = new HttpHost("tunnel5.qg.net", 15931);
RequestConfig requestConfig = RequestConfig.custom().setProxy(myProxy).build();
HttpPost post = new HttpPost(url);
post.addHeader("content-type", "application/json");
StringEntity postingString = new StringEntity(param.toString());
post.setConfig(requestConfig);
post.setEntity(postingString);
CloseableHttpResponse response = httpclient.execute(post);
byte[] bResultXml = EntityUtils.toByteArray(response.getEntity());
String result = new String(bResultXml, "utf-8");
System.out.println(index + "\t" + result);
}
private static final String HTTP = "http";
private static final String HTTPS = "https";
private static SSLConnectionSocketFactory sslsf = null;
private static PoolingHttpClientConnectionManager cm = null;
private static SSLContextBuilder builder = null;
static {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
try {
builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
sslsf = new SSLConnectionSocketFactory(builder.build(),
new String[] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
Registry registry = RegistryBuilder.create()
.register(HTTP, new PlainConnectionSocketFactory()).register(HTTPS, sslsf).build();
cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(200);// max connection
} catch (Exception e) {
e.printStackTrace();
}
}
}