使用 Java 将 XML 数据发布到 API 时出现问题
我在使用 HTTP POST 向 API 发送 XML 数据时遇到问题。
如果我发送格式正确的 XML,我会收到一条错误消息:
服务器异常:无法访问关闭的流
如果 XML 格式不正确,我会收到 HTTP 500
。如果我只是发送一个空字符串而不是带有 XML 的字符串,我会收到一条错误消息:EMPTY REQUEST
。
我对错误可能是什么没有太多想法,但连接有效,因为错误消息以 XML 格式返回。我只是将 XML 数据作为字符串发送。最后是否有可能要求我发送EOF或某些内容?我该如何在我的 Java 代码中做到这一点?关于问题可能是什么还有其他想法吗?
API 是用 .NET 编写的,
下面是我用来 POST XML 数据的 Java 代码:
Authenticator.setDefault(new MyAuthenticator());
String xmlRequestStatus =
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><test><data>32</data></test>";
System.out.println(xmlRequestStatus);
String contentType = "text/xml";
String charset = "ISO-8859-1";
String request = null;
URL url = null;
HttpURLConnection connection = null;
OutputStream output = null;
InputStream response = null;
try {
url = new URL("http://127.0.0.1/test");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", contentType);
output = connection.getOutputStream();
output.write(request.getBytes("ISO-8859-1"));
if(output != null) try { output.close(); } catch (IOException e) {}
response = connection.getInputStream();
....
I'm having problem with sending XML-data using HTTP POST to an API.
If I send well formatted XML, I get an error message:
Server Exception: Cannot access a closed Stream
If the XML isn't well formatted, I get HTTP 500
. And if I just send an empty string instead of a string with XML, I get back an error message: EMPTY REQUEST
.
I don't have many ideas about what the error could be, but the connection works because the error message is returned in XML format. I'm just sending the XML data as a string. Is it possible that I am required to send an EOF or something in the end? And how do I do that in my Java code? Any other ideas about what the problem can be?
The API is made in .NET
Here is the Java code I'm using to POST the XML data:
Authenticator.setDefault(new MyAuthenticator());
String xmlRequestStatus =
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><test><data>32</data></test>";
System.out.println(xmlRequestStatus);
String contentType = "text/xml";
String charset = "ISO-8859-1";
String request = null;
URL url = null;
HttpURLConnection connection = null;
OutputStream output = null;
InputStream response = null;
try {
url = new URL("http://127.0.0.1/test");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", contentType);
output = connection.getOutputStream();
output.write(request.getBytes("ISO-8859-1"));
if(output != null) try { output.close(); } catch (IOException e) {}
response = connection.getInputStream();
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它看起来不错并且应该可以正常工作。然而,当您已经执行
connection.setDoOutput(true);
时,connection.setRequestMethod("POST");
就完全是多余的了。由于此错误直接来自本地主机上托管的 .NET Web 服务,您确定它的编写没有错误吗?我不使用 .NET,但是 Google 了解到它与
MemoryStream
有关。我会专注于 .NET 代码并重新测试/调试它。也许那些相关的SO问题可能会有所帮助。It looks fine and should work fine. The
connection.setRequestMethod("POST");
is however entirely superfluous when you already didconnection.setDoOutput(true);
.Since this error is coming straight from the .NET webservice hosted at localhost, are you sure that it is written without bugs? I don't do .NET, but Google learns me that it's related to
MemoryStream
. I'd concentrate on the .NET code and retest/debug it. Maybe those related SO questions may help.您需要通过执行类似这样的操作来指定方法 POST,
否则,它会被视为 GET,并且某些服务器不希望正文包含 GET,因此流会被关闭。
You need to specify method POST by doing something like this,
Otherwise, it's treated as a GET and some server doesn't expect body with GET so the stream is closed.
也许稍后会在控制流中关闭 OutputStream。因此,而不是这个:
尝试这个(也许添加冲洗)?
Maybe close the OutputStream later in the control flow. So instead of this:
Try this (and maybe add the flush)?
难道不应该是
<32
而不是<32
吗?Shouldn't it be
<32
instead of<32
?看起来 request 被初始化为 null,但之后没有设置。难道不是吗
It looks like request is initialized to null, but afterwards not set. Should it not be