问题描述
我使用HttpURLConnection连接到网站并收到ResponseCode = 404(HTTP_NOT_FOUND)。但是我在浏览器(IE)中打开网站没有问题。
I use a HttpURLConnection to connect to a website and receive an ResponseCode=404 (HTTP_NOT_FOUND). However I have no problem opening the website in my browser (IE).
为什么会有差异,我该怎么办?
Why the difference, and what can I do about it?
问候,Pavan
这是我的计划
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class TestGet {
private static URL source;
public static void main(String[] args) {
doGet();
}
public static void doGet() {
try {
source = new URL("http://localhost:8080/");
System.out.println("Url is" + source.toString());
URLConnection connection = source.openConnection();
connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
connection.setRequestProperty("Accept","*/*");
connection.setDoInput(true);
connection.setDoOutput(true);
System.out.println(((HttpURLConnection) connection).getResponseCode());
BufferedReader rdr = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
StringBuffer b = new StringBuffer();
String line = null;
while (true) {
line = rdr.readLine();
if (line == null)
break;
b.append(line);
}
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.toString());
}
}
}
Stack Trace
Stack Trace
Url ishttp://localhost:8080/
404
java.io.FileNotFoundException: http://localhost:8080/
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at TestGet.doGet(TestGet.java:28)
at TestGet.main(TestGet.java:11)
Caused by: java.io.FileNotFoundException: http://localhost:8080/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at TestGet.doGet(TestGet.java:26)
... 1 more
java.io.FileNotFoundException: http://localhost:8080/
推荐答案
您收到404错误,表示找不到请求的响应。首先,您需要确保服务器在 http:// localhost:8080 /
服务,并且必须返回一些代码为200的内容。如果没有,则有我们无法帮助您。
You are getting 404 error that means the response for the request is not found. First you need to make sure that there is a server serving at http://localhost:8080/
and it must return some content with code 200. If not, then there is nothing we can help you.
测试网址上是否有任何内容的最简单方法是将网址粘贴到网络浏览器地址栏上,然后点击开始。但是,这并不能保证Java代码能够访问它。例如,如果服务器设计为在无法找到Web浏览器 User-Agent
标头时响应404。
The easiest way to test whether there is anything at the url is to paste the url on the web browser address bar and click go. However, this does not guarantee that the Java code will be able to access it. For example, if the server is designed to response 404 if it cannot find the web browser User-Agent
header.
由于服务器返回状态代码(200或404),这意味着这不是防火墙问题。
Since the server returns a status code, either 200 or 404, it means this is not a firewall problem.
根据您最新版本的问题,您可以使用Web浏览器查看它,但无法使用您的Java代码下载它,并且标题似乎设置正确。我只能看到两个问题:
According to your latest edition of the question, you can view it with the web browser but cannot download it with your java code and the header seems to be set correctly. There are only two problem I can see:
-
你不应该设置
connection.setDoOutput(true) ;
为true。这将强制连接以执行HTTP POST而不是GET,并且服务器可能不支持POST。
You should not set
connection.setDoOutput(true);
to true. This will enforce the connection to do HTTP POST instead of GET and the server may not support POST.
您的服务器可能总是返回404,即使它应该有已经200.由于Web浏览器不关心错误状态并尝试呈现所有内容,因此它似乎在Web浏览器中工作。如果是这样,您应该先修复服务器以正确响应,否则请尝试获取错误流而不是
Your server may be always returning 404 even if it should have been 200. Since the web browser doesn't care about the error status and tries to render all the content so it seems to be working from the web browser. If so, you should fix the server to reponse correctly first, otherwise try getting error stream instead HttpURLConnection#getErrorStream()
这篇关于URL可以通过浏览器访问,但仍然可以通过URLConnection访问FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!