java 中URL访问报异常Server returned HTTP response code: 403 for URL,网上说设置 User-Agent解决无果
URLurl=newURL("http://img0.imgtn.bdimg.com/it/u=821748160,1851443036&fm=21&gp=0.jpg")...
URL url = new URL("http://img0.imgtn.bdimg.com/it/u=821748160,1851443036&fm=21&gp=0.jpg");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 5.0;Windows NT;DigExt)");
InputStream errorStream = null;
BufferedReader br = null;
InputStream is = null;
String err = null;
try {
System.out.println(con.getRequestProperties());
errorStream = con.getErrorStream();
if (errorStream != null) {
br = new BufferedReader(new InputStreamReader(errorStream));
while ((err = br.readLine()) != null) {
System.out.println(err);
}
}
is = con.getInputStream();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (errorStream != null) {
errorStream.close();
}
if (is != null) {
is.close();
}
}
试了多种多样的 User-Agent 属性,结果都不对,仍是在得到输入流的时候抛出Server returned HTTP response code: 403 for URL异常,求解决 展开
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 5.0;Windows NT;DigExt)");
InputStream errorStream = null;
BufferedReader br = null;
InputStream is = null;
String err = null;
try {
System.out.println(con.getRequestProperties());
errorStream = con.getErrorStream();
if (errorStream != null) {
br = new BufferedReader(new InputStreamReader(errorStream));
while ((err = br.readLine()) != null) {
System.out.println(err);
}
}
is = con.getInputStream();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (errorStream != null) {
errorStream.close();
}
if (is != null) {
is.close();
}
}
试了多种多样的 User-Agent 属性,结果都不对,仍是在得到输入流的时候抛出Server returned HTTP response code: 403 for URL异常,求解决 展开
若以下回答无法解决问题,邀请你更新回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询