我有2个设备,访问同一个URL,但是返回的结果却不同,我怎么才能保持一致?我感觉是User-Agent 导致的这个返回结果不同!于是我尝试修改User-Agent
URLConnection con = url.openConnection();
con.setConnectTimeout(25 * 1000);
//我想骗过服务器,让服务器觉得设备是通过电脑的方式访问
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
但是依然没解决希望大家多提提建议 因为我现在毫无思路~
URLConnection con = url.openConnection();
con.setConnectTimeout(25 * 1000);
//我想骗过服务器,让服务器觉得设备是通过电脑的方式访问
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
但是依然没解决希望大家多提提建议 因为我现在毫无思路~
private static String getImageText(String url);{
try {
URL u=new URL(url);;
URLConnection uc=u.openConnection();;
//要做足功夫,不然豆瓣就会拒绝你的请求
uc.setRequestProperty("Host","www.douban.com");;
uc.setRequestProperty("Accept","*/*");;
uc.setRequestProperty("Referer","http://www.douban.com/");;
uc.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.00; Windows 98);");;
uc.setRequestProperty("Pragma","no-cache");;
uc.setRequestProperty("Cache-Control","no-cache");;
uc.setRequestProperty("Connection","close");;
//StreamUtils是一个辅助工具,就不再给出了
return new String(StreamUtils.read(uc.getInputStream(););,"utf-8");;
} catch (Exception e); {
return "";
}
}
setRequestProperty设置的全面一些,参考:http://www.iteye.com/topic/17023