java.io.IOException:服务器返回 HTTP 响应代码:URL 403

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30092798/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:08:03  来源:igfitidea点击:

java.io.IOException: Server returned HTTP response code: 403 for URL

javahttphttp-status-code-403http-status-codes

提问by Ritesh Sinha

I want to open a link from url : "http://www.kohls.com/search.jsp?search=Hymanet&submit-search=web-regular", sometimes i get:

我想从 url 打开一个链接:“ http://www.kohls.com/search.jsp?search=Hymanet&submit-search=web-regular”,有时我得到:

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:

java.io.IOException:服务器返回 HTTP 响应代码:URL 为 403。但是用浏览器打开网址就可以了。以下是我的代码的一部分:

URL url = new URL("http://www.kohls.com/search.jsp?search=Hymanet&submit-search=web-regular");

InputStream is = url.openConnection().getInputStream();

error detail

错误详情

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.kohls.com/search.jsp?N=0&search=Hymanet&WS=96at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627) at Links.main(Links.java:41)

线程“main”中的异常 java.io.IOException:服务器返回 HTTP 响应代码:URL 为 403:http: //www.kohls.com/search.jsp?N =0&search =Hymanet&WS =96 at sun.net.www。协议.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627) 在 Links.main(Links.java:41)

采纳答案by ktorn

The particular webserver you are trying to access is checking the User-AgentHTTP header and denying access to anything that doesn't look like a normal browser, to prevent bots (which is probably what you are writing).

您尝试访问的特定网络服务器正在检查User-AgentHTTP 标头并拒绝访问任何看起来不像普通浏览器的内容,以防止机器人(这可能就是您正在编写的内容)。

You just need to set the header as part of your request in Java and it will work.

您只需要在 Java 中将标头设置为请求的一部分,它就会起作用。

How you set the header will depend on how you are making the connection, but if you are using a simple URLConnection then this will work:

您如何设置标头取决于您如何建立连接,但如果您使用的是简单的 URLConnection,那么这将起作用:

URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0");

Normally a "real" User-Agentcontains lots of extra info, but that webserver seems to look only for the basic browser type.

通常“真实”User-Agent包含大量额外信息,但该网络服务器似乎只查找基本浏览器类型。

You can prove this using wgetwith and without the -UUser-Agent option:

您可以使用wget和不使用-UUser-Agent 选项来证明这一点:

$ wget "http://www.kohls.com/search.jsp?search=Hymanet&submit-search=web-regular"
--2015-05-07 16:08:46--  http://www.kohls.com/search.jsp?search=Hymanet&submit-search=web-regular
2015-05-07 16:08:46 ERROR 403: Forbidden.

$ wget -U "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0" "http://www.kohls.com/search.jsp?search=Hymanet&submit-search=web-regular"
--2015-05-07 16:08:49--  http://www.kohls.com/search.jsp?search=Hymanet&submit-search=web-regular
awaiting response... 200 OK
...