java.io.IOException:服务器返回 HTTP 响应代码:URL 403
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3869372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.IOException: Server returned HTTP response code: 403 for URL
提问by Adao
I want to download the mp3 file from url : "http://upload13.music.qzone.soso.com/30671794.mp3", i always got java.io.IOException: Server returned HTTP response code: 403 for URL. But it's ok when open the url using browser. Below is part of my code:
我想从 url 下载 mp3 文件:“http://upload13.music.qzone.soso.com/30671794.mp3”,我总是收到 java.io.IOException:服务器返回 HTTP 响应代码:URL 的 403。但是用浏览器打开网址就可以了。以下是我的代码的一部分:
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
URL url = new URL(link);
URLConnection urlConn = url.openConnection();
urlConn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
String contentType = urlConn.getContentType();
System.out.println("contentType:" + contentType);
InputStream is = urlConn.getInputStream();
bis = new BufferedInputStream(is, 4 * 1024);
bos = new BufferedOutputStream(new FileOutputStream(
fileName.toString()));?
Anyone could help me? Thanks in advance!
任何人都可以帮助我吗?提前致谢!
回答by Alexander Sagen
When I access the URL with my browser I also get 403. Perhaps you're logged in to the site with your browser?
当我使用浏览器访问 URL 时,我也会收到 403。也许您使用浏览器登录了该站点?
If that's the case you need to duplicate the cookie from your browser and send it along, perhaps even do more to replicate your browser's signature if the site does any extra checks.
如果是这种情况,您需要从浏览器复制 cookie 并将其发送,如果站点进行任何额外检查,甚至可以做更多来复制浏览器的签名。
You can set the cookie by adding:
您可以通过添加以下内容来设置 cookie:
urlConn.setRequestProperty("Cookie", "foo=bar");
Where foo=bar is the key-value pair you'll find when you locate the site's cookie in your browser.
其中 foo=bar 是您在浏览器中找到站点的 cookie 时会找到的键值对。
回答by Saratha
Instead of using URLConnection
in java, if you use HttpURLConnection
you should beable to access the requested web page from java. Try the following code:
而不是URLConnection
在java中使用,如果你使用HttpURLConnection
你应该能够从java访问请求的网页。试试下面的代码:
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
Normal java using urlConnection
wont be accepted to access the internet. To access the browser it will need to perform a search without theexception HTTP response code : 403 for URL
使用普通 javaurlConnection
不会被接受访问互联网。要访问浏览器,它需要无例外地执行搜索HTTP response code : 403 for URL
EDIT (@Mordechai): No need to do the casting, just add the user agent.
编辑(@Mordechai):无需进行转换,只需添加用户代理即可。
回答by arthur
The problem is given by the Status code. 403 means actually "Forbidden" and implies The request was denied for a reason the server does not want to (or has no means to) indicate to the client.
问题由状态代码给出。403实际上意味着“禁止”,并暗示由于服务器不想(或无法)向客户端指示的原因,请求被拒绝。
the problem lies at the server-side.
问题出在服务器端。
回答by runlevel0
I would also check if the server were the resource is located has an ACL or similar in place, we just resolved a "java.io.IOException: 403" issue this way.
我还会检查资源所在的服务器是否有 ACL 或类似的地方,我们只是通过这种方式解决了“java.io.IOException: 403”问题。
It happens that 403 errors are very generic and you cannot really be sure of the source as it can be just anything.
碰巧 403 错误非常普遍,您无法确定来源,因为它可以是任何东西。
回答by Montezuma
You can also use
你也可以使用
System.setProperty("http.agent", "Chrome");
it worked for me.
它对我有用。
//Update
//更新
Explanation
解释
Because HttpURLConnection reads the property "http.agent" if set. You can read it here: https://www.innovation.ch/java/HTTPClient/advanced_info.html
因为 HttpURLConnection 读取属性“http.agent”(如果设置)。你可以在这里阅读:https: //www.innovation.ch/java/HTTPClient/advanced_info.html
Or you can look it up in the source code of the HttpURLConnection Class:
或者你可以在 HttpURLConnection 类的源代码中查找:
String agent = java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("http.agent"));
String agent = java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("http.agent"));