import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.SecureRandom;
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;
import java.util.Map.Entry;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.helper.HttpConnection;
import org.jsoup.select.Elements;
public class GetTujiaHtml {
public static String testPost(String urlStr,String param,String id) {
try {
URL url = new URL(urlStr);
URLConnection con1 = url.openConnection();
HttpURLConnection con = (HttpURLConnection)con1;
// 发送POST请求必须设置如下两行
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/json, text/plain, */*");
con.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
con.setRequestProperty("Cookie", "__guid=195168682.753543604333822000.1532336993597.7224; tujia.com_PortalContext_RefUrl=https://www.tujia.com/gongyu/sanya/hp2/; tujia.com_PortalContext_LongerRefUrl=https://www.tujia.com/gongyu/sanya/hp2/; tujia.com_PortalContext_GuestToken=9ce24447-88df-4203-9a0a-d0099e9c8909; tujia.com_PortalContext_GuestId=-1002633651; tujia.com_PortalContext_LandingUrl=http://www.tujia.com/UI/Float?callback=TJPFloating.BuildHTML&PlatformType=pc; manualclose=1; tujia.com_PortalContext_UserClickKeywordRecord=; tujia.com_PortalContext_UserSessionId=e24aac71-40be-4038-a335-666d04863f58; JSESSIONID=33191E49AED12512A01F9B589BE3EE92; tujia.com_PortalContext_DestinationId=1; monitor_count=12; tujia.com_PortalContext_StartDate=2018-7-24; tujia.com_PortalContext_EndDate=2018-7-25; gr_user_id=9e67650c-c645-43b5-a92c-8f31cebad486; _ga=GA1.2.605216747.1532336995; _gid=GA1.2.557490446.1532336995; Hm_lvt_405c96e7f6bed44fb846abfe1f65c6f5=1532336995; Hm_lpvt_405c96e7f6bed44fb846abfe1f65c6f5=1532401783; accessId=797098a0-b29d-11e5-b3b1-49764155fe50; pageViewNum=11; bad_id797098a0-b29d-11e5-b3b1-49764155fe50=2b8f7bd1-8e58-11e8-a1e8-d587156eb665; nice_id797098a0-b29d-11e5-b3b1-49764155fe50=2b8f7bd2-8e58-11e8-a1e8-d587156eb665; tujia.com_PortalContext_UserId=0; gr_session_id_1fa38dc3b3e047ffa08b14193945e261=80ebee6f-ca30-4970-bd76-3ae658c0fc0e; gr_session_id_1fa38dc3b3e047ffa08b14193945e261_80ebee6f-ca30-4970-bd76-3ae658c0fc0e=false");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
con.setRequestProperty("Host", "www.tujia.com");
con.setRequestProperty("Origin", "https://www.tujia.com");
con.setRequestProperty("Referer", "https://www.tujia.com/detail/"+id+".htm");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
OutputStreamWriter out = new OutputStreamWriter(con
.getOutputStream());
//String xmlInfo = getXmlInfo();
//System.out.println("urlStr=" + urlStr);
//System.out.println("xmlInfo=" + xmlInfo);
//out.write("{\"getStoreInfosSearchDTO\":{\"appId\":\"b3ad5aba-ae1e-4f4b-ba55-c5fb0375de3c\",\"areaCode\":\"3-110112\",\"filter\":{\"areaCode\":\"\",\"distance\":\"\",\"govGrade\":\"\",\"marketCode\":\"\",\"restaurantType\":\"\",\"tradeAreaCode\":\"\"},\"lat\":\"39.966983795166\",\"lng\":\"116.30883026123\",\"pageIndex\":"+pageIndex+",\"pageSize\":20,\"userId\":\"a6b75273-0153-44e4-be0d-29cf006ebb22\"}}");
out.write(param);
//out.write(new String(xmlInfo.getBytes("ISO-8859-1")));
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(con
.getInputStream()));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
//System.out.println(line);
return line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String GetSearch2(String param,String page,String id) throws Exception {
String body = "";
HttpPost httpPost = new HttpPost(
"https://www.tujia.com/bingo/pc/comment/searchUnitComments");
CloseableHttpClient client = HttpClients.createDefault();
// Map
// map.put("hasPic", "false");
// map.put("pageIndex", page);
// map.put("pageSize", "10");
// map.put("unitId", id);
// map.put("recommended", "false");
// // 装填参数
// List
// if (map != null) {
// for (Entry
// nvps.add(new BasicNameValuePair(entry.getKey(), entry
// .getValue()));
// }
// }
// // 设置参数到请求对象中
// httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 设置header信息
// 指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("Accept", "application/json, text/plain, */*");
httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
httpPost.setHeader("Cookie", "__guid=195168682.753543604333822000.1532336993597.7224; tujia.com_PortalContext_RefUrl=https://www.tujia.com/gongyu/sanya/hp2/; tujia.com_PortalContext_LongerRefUrl=https://www.tujia.com/gongyu/sanya/hp2/; tujia.com_PortalContext_GuestToken=9ce24447-88df-4203-9a0a-d0099e9c8909; tujia.com_PortalContext_GuestId=-1002633651; tujia.com_PortalContext_LandingUrl=http://www.tujia.com/UI/Float?callback=TJPFloating.BuildHTML&PlatformType=pc; manualclose=1; tujia.com_PortalContext_UserClickKeywordRecord=; tujia.com_PortalContext_UserSessionId=e24aac71-40be-4038-a335-666d04863f58; JSESSIONID=33191E49AED12512A01F9B589BE3EE92; tujia.com_PortalContext_DestinationId=1; monitor_count=12; tujia.com_PortalContext_StartDate=2018-7-24; tujia.com_PortalContext_EndDate=2018-7-25; gr_user_id=9e67650c-c645-43b5-a92c-8f31cebad486; _ga=GA1.2.605216747.1532336995; _gid=GA1.2.557490446.1532336995; Hm_lvt_405c96e7f6bed44fb846abfe1f65c6f5=1532336995; Hm_lpvt_405c96e7f6bed44fb846abfe1f65c6f5=1532401783; accessId=797098a0-b29d-11e5-b3b1-49764155fe50; pageViewNum=11; bad_id797098a0-b29d-11e5-b3b1-49764155fe50=2b8f7bd1-8e58-11e8-a1e8-d587156eb665; nice_id797098a0-b29d-11e5-b3b1-49764155fe50=2b8f7bd2-8e58-11e8-a1e8-d587156eb665; tujia.com_PortalContext_UserId=0; gr_session_id_1fa38dc3b3e047ffa08b14193945e261=80ebee6f-ca30-4970-bd76-3ae658c0fc0e; gr_session_id_1fa38dc3b3e047ffa08b14193945e261_80ebee6f-ca30-4970-bd76-3ae658c0fc0e=false");
httpPost.setHeader("Connection", "keep-alive");
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setHeader("Host", "www.tujia.com");
httpPost.setHeader("Origin", "https://www.tujia.com");
httpPost.setHeader("Referer", "https://www.tujia.com/detail/"+id+".htm");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
//设置请求体
httpPost.setEntity(new StringEntity(param));
// RequestConfig requestConfig = RequestConfig.custom()
// // .setProxy(proxy)
// .setConnectTimeout(10000).setSocketTimeout(10000)
// .setConnectionRequestTimeout(3000).build();
// // httpPost.setConfig(requestConfig);
// 执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
// 获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
// 释放链接
response.close();
System.out.println(body);
//Document doc = Jsoup.parse(body);
return body;
}
public static void main(String[] args) {
try {
String id ="17825";
String param="{\"hasPic\": false, \"pageIndex\": 2, \"pageSize\": 10, \"unitId\": \"17825\",\"recommended\": false}";
String html =GetSearch2("https://www.tujia.com/bingo/pc/comment/searchUnitComments",param,id);
System.out.println(html);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}