从 HttpURLConnection 获取 InputStream 对象时出现 FileNotFoundException
我正在尝试使用 HttpURLConnection (用于在 java 中使用 cUrl)向 url 发送 post 请求。 请求的内容是 xml,在终点,应用程序处理 xml 并将记录存储到数据库,然后以 xml 字符串的形式发回响应。该应用程序本地托管在 apache-tomcat 上。
当我从终端执行此代码时,一行会按预期添加到数据库中。但是从连接获取InputStream时会抛出异常,
java.io.FileNotFoundException: http://localhost:8080/myapp/service/generate
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)
at org.kodeplay.helloworld.HttpCurl.main(HttpCurl.java:30)
如下是代码
public class HttpCurl {
public static void main(String [] args) {
HttpURLConnection con;
try {
con = (HttpURLConnection) new URL("http://localhost:8080/myapp/service/generate").openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
File xmlFile = new File("test.xml");
String xml = ReadWriteTextFile.getContents(xmlFile);
con.getOutputStream().write(xml.getBytes("UTF-8"));
InputStream response = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
for (String line ; (line = reader.readLine()) != null;) {
System.out.println(line);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
它令人困惑,因为异常被追踪到行InputStream response = con.getInputStream();
并且似乎没有是 FileNotFoundException 涉及的任何文件。
当我尝试直接打开与 xml 文件的连接时,它不会抛出此异常。
服务应用程序使用 spring 框架和 Jaxb2Marshaller 来创建响应 xml。
ReadWriteTextFile 类取自此处
谢谢。
编辑: 它会将数据保存在数据库中,同时发回 404 响应状态码。
我还尝试使用 php 进行卷曲并打印出 CURLINFO_HTTP_CODE
,结果是 200。
关于如何调试这个有什么想法吗?服务和客户端都在本地服务器上。
已解决: 我可以在参考 答案 就 SO 本身而言。
当使用非标准端口连接到 url 时,HttpURLConnection 似乎总是返回 404 响应。
添加这些行解决了它
con.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
con.setRequestProperty("Accept","*/*");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我不知道你的 Spring/JAXB 组合,但一般的 REST Web 服务不会在 POST/PUT 上返回响应正文,只是一个 响应状态。你想确定它而不是身体。
替换
为
HTTP 规范中提供了所有可用的状态代码及其含义,如前面链接的。 Web 服务本身还应该附带一些文档,概述 Web 服务支持的所有状态代码及其特殊含义(如果有)。
如果状态以
4nn
或5nn
开头,您需要使用getErrorStream()
来读取可能包含错误的响应正文细节。I don't know about your Spring/JAXB combination, but the average REST webservice won't return a response body on POST/PUT, just a response status. You'd like to determine it instead of the body.
Replace
by
All available status codes and their meaning are available in the HTTP spec, as linked before. The webservice itself should also come along with some documentation which overviews all status codes supported by the webservice and their special meaning, if any.
If the status starts with
4nn
or5nn
, you'd like to usegetErrorStream()
instead to read the response body which may contain the error details.FileNotFound
只是一个不幸的异常,用于指示 Web 服务器返回了 404。FileNotFound
is just an unfortunate exception used to indicate that the web server returned a 404.对于将来遇到此问题的任何人来说,原因是状态代码是 404(或者在我的情况下是 500)。当状态代码不是 200 时,
InpuStream
函数似乎会抛出错误。在我的例子中,我控制自己的服务器并返回 500 状态代码以指示发生了错误。尽管我还发送了带有详细说明错误的字符串消息的正文,但无论正文完全可读,
inputstream
都会抛出错误。如果你控制你的服务器,我想这可以通过向自己发送 200 状态代码然后处理任何字符串错误响应来处理。
To anyone with this problem in the future, the reason is because the status code was a 404 (or in my case was a 500). It appears the
InpuStream
function will throw an error when the status code is not 200.In my case I control my own server and was returning a 500 status code to indicate an error occurred. Despite me also sending a body with a string message detailing the error, the
inputstream
threw an error regardless of the body being completely readable.If you control your server I suppose this can be handled by sending yourself a 200 status code and then handling whatever the string error response was.
对于其他遇到此问题的人来说,在尝试将 SOAP 请求标头发送到 SOAP 服务时,我也遇到了同样的情况。问题是代码中的顺序错误,我在发送 XML 正文之前先请求输入流。在下面的代码片段中,
InputStream in = conn.getInputStream();
行紧接在ByteArrayOutputStream out = new ByteArrayOutputStream();
之后,这是错误的顺序。在本例中,
FileNotFound
是一种不幸的 HTTP 响应代码 400 编码方式。For anybody else stumbling over this, the same happened to me while trying to send a SOAP request header to a SOAP service. The issue was a wrong order in the code, I requested the input stream first before sending the XML body. In the code snipped below, the line
InputStream in = conn.getInputStream();
came immediately afterByteArrayOutputStream out = new ByteArrayOutputStream();
which is the incorrect order of things.FileNotFound
in this case was an unfortunate way to encode HTTP response code 400.在这种情况下,FileNotFound 意味着您从服务器收到 404 - 可能是服务器不喜欢“POST”请求?
FileNotFound in this case means you got a 404 from your server - could it be that the server does not like "POST" requests?
在这种情况下,FileNotFound 意味着您从服务器收到 404
您必须设置请求内容类型标头参数
将“content-type”请求头设置为“application/json”,以 JSON 形式发送请求内容。
必须设置此参数才能以 JSON 格式发送请求正文。
如果不这样做,服务器将返回 HTTP 状态代码“400-bad request”。
con.setRequestProperty("Content-Type", "application/json; utf-8");
完整脚本 ->
并将其命名为
new SendDeviceDetails().execute("");
您可以在本教程中找到更多详细信息
https://www.baeldung.com/httpurlconnection-post
FileNotFound in this case means you got a 404 from your server
You Have to Set the Request Content-Type Header Parameter
Set “content-type” request header to “application/json” to send the request content in JSON form.
This parameter has to be set to send the request body in JSON format.
Failing to do so, the server returns HTTP status code “400-bad request”.
con.setRequestProperty("Content-Type", "application/json; utf-8");
Full Script ->
and call it
new SendDeviceDetails().execute("");
you can find more details in this tutorial
https://www.baeldung.com/httpurlconnection-post
解决方案:
只需将 localhost 更改为您 PC 的 IP
如果你想知道这一点:Windows+r > cmd> ipconfig
示例:http://192.168.0.107/directory/service/program.php?action=sendSomething
只需将 192.168.0.107 替换为您自己的 IP(不要尝试 127.0.0.1,因为它与 localhost 相同)
The solution:
just change localhost for the IP of your PC
if you want to know this: Windows+r > cmd > ipconfig
example: http://192.168.0.107/directory/service/program.php?action=sendSomething
just replace 192.168.0.107 for your own IP (don't try 127.0.0.1 because it's same as localhost)
请更改
为
Please change
To