HttpClient与浏览器请求的结果不一样?

chally 发布于 2014/12/05 16:57
阅读 3K+
收藏 0

用百度的快递查询API,在浏览器上访问能正常返回信息。

但是使用HttpClient 返回的信息始终都是一样的。

开始怀疑是服务器根据请求信息来判断是不是浏览器发出的请求,于是修改了代码里的请求头信息,已经尽量跟浏览器保持一致了,结果依然不行。

请问大神们这里还有什么需要注意的地方?

URL在这里:http://api.open.baidu.com/pae/channel/data/asyncqury?cb=jQuery1102022016299865208566_1417686284549&appid=4001&com=shentong&nu=123123123123&_=1417767163200
    HttpClient client = HttpClients.createDefault();
			String ur = IAPIConfig.URL;
			HttpGet get = new HttpGet(ur);
			get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
//			get.setHeader("Host", "api.open.baidu.com");
			get.setHeader("Accept-Encoding", "gzip,deflate,sdch");
			get.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
			get.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
			get.setHeader("cache-control", "max-age=0");
			
			HttpEntity e= client.execute(get).getEntity();
			InputStream in = e.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
	          String line = "";
	           while ((line = br.readLine()) != null) {
	            System.out.println(line);
	           }
	           br.close();



以下是问题补充:

@chally:浏览器请求的结果: /**/jQuery1102022016299865208566_1417686284549({"msg":"申通 单号123123123123,没有查到相关信息。单号暂未收录或已过期","status":"-3","data":{"info":{"status":"0","msg":"申通 单号123123123123,没有查到相关信息。单号暂未收录或已过期","_source_com":""},"com":"shentong","company":{"url":"http:\/\/www.kuaidi100.com\/all\/st.shtml?from=openv","fullname":"申通快递","shortname":"申通","icon":{"id":"15","smallurl":"http:\/\/t11.baidu.com\/it\/u=1645020595,3670072571&fm=58","smallpos":"0,16","middleurl":"http:\/\/t11.baidu.com\/it\/u=2002061548,3757200329&fm=58","middlepos":"0,1008","normal":"http:\/\/t11.baidu.com\/it\/u=3644049925,887921644&fm=58"},"website":{"title":"www.sto.cn","url":"http:\/\/www.sto.cn"},"tel":"400-889-5543","auxiliary":[{"title":"网点查询","url":"http:\/\/www.kuaidi100.com\/network\/province_5.htm?from=openv"},{"title":"网上寄件","url":"http:\/\/www.kuaidi100.com\/orderIndex.jsp?from=openv"}]},"source":{"logo":"http:\/\/t12.baidu.com\/it\/u=1429564979,1787167512&fm=58","title":"数据来自快递100","url":"http:\/\/www.kuaidi100.com\/","name":"快递100"}}}) client请求的结果: jQuery1102022016299865208566_1417686284549({"msg":"该单号暂无物流进展,请稍后再试,或检查公司和单号是否有误。","status":"-3","data":{"info":{"status":"0","msg":"该单号暂无物流进展,请稍后再试,或检查公司和单号是否有误。","_source_com":""},"com":"shentong","company":{"url":"http:\/\/www.kuaidi100.com\/all\/st.shtml?from=openv","fullname":"申通快递","shortname":"申通","icon":{"id":"15","smallurl":"http:\/\/t11.baidu.com\/it\/u=1645020595,3670072571&fm=58","smallpos":"0,16","middleurl":"http:\/\/t11.baidu.com\/it\/u=2002061548,3757200329&fm=58","middlepos":"0,1008","normal":"http:\/\/t11.baidu.com\/it\/u=3644049925,887921644&fm=58"},"website":{"title":"www.sto.cn","url":"http:\/\/www.sto.cn"},"tel":"400-889-5543","auxiliary":[{"title":"网点查询","url":"http:\/\/www.kuaidi100.com\/network\/province_5.htm?from=openv"},{"title":"网上寄件","url":"http:\/\/www.kuaidi100.com\/orderIndex.jsp?from=openv"}]},"source":{"logo":"http:\/\/t12.baidu.com\/it\/u=1429564979,1787167512&fm=58","title":"数据来自快递100","url":"http:\/\/www.kuaidi100.com\/","name":"快递100"}}}) 都是转码后的信息,client怎么设参数返回的信息始终一样,错误信息也与浏览器的不一致。 (2014/12/05 17:51)
加载中
0
Altman
Altman
LZ你逗我吧,我用curl和wget都下下来了你告诉我你跟ua有关系啊
Altman
Altman
回复 @chally : 一样的啊
chally
chally
你用curl与wget请求的结果与 浏览器一致?
0
y
ysn521
禁用一下http的缓存试试
0
JeffreyLin
JeffreyLin
换个IP试试
0
南湖船老大
南湖船老大
用 wireshark 抓包
0
chally
chally

谢谢大家!

解决了,cookie的问题。

sipt
sipt
能说详细点么?
返回顶部
顶部