80,362
社区成员
发帖
与我相关
我的任务
分享
URLConnection con = url.openConnection();
con.setConnectTimeout(25 * 1000);
//我想骗过服务器,让服务器觉得设备是通过电脑的方式访问
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
# //根据url,取得文字
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 "";
}
}