简体   繁体   English

需要从 java 发送一个 xml 请求并解析 xml 响应

[英]Need to send a xml request from java and parse the xml response

I need to send a xml request from a java standalone class.我需要从 java 独立 class 发送 xml 请求。 The xml request that I need to send is of the form -我需要发送的 xml 请求的形式为 -

http://url/query.do?
Request=<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Request initialTime="2011-03-11T16:40Z">
<Query>Java</Query></Request>

So i have put in所以我已经投入

String xmlRequest = "url/query.do? Request=<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Request initialQueryTime="2011-03-11T16:40Z"> <Query>Java</Query></Request>"

and then接着

URL url = new URL(xmlRequest);
     URLConnection conn = (URLConnection)url.openConnection();
     //conn.connect();
     BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    conn.getInputStream()));

But I am getting a IOException - Server returned HTTP response code: 505 for URL at sun.net.www.protocol.http.HttpURLConnection.getInputStream.但我收到IOException - Server returned HTTP response code: 505 for URL at sun.net.www.protocol.http.HttpURLConnection.getInputStream.

This url when hit through a browser returns a xml response and I need to parse that XML using STAX.当通过浏览器点击此 url 时返回 xml 响应,我需要使用 STAX 解析该 XML。

Can anyone please provide some idea on how to achieve the above.谁能提供一些关于如何实现上述目标的想法。

Can someone please provide some sample code or correct my code so that I am able to form the xml request and parse the xml response.有人可以提供一些示例代码或更正我的代码,以便我能够形成 xml 请求并解析 xml 响应。 Please help.请帮忙。

Thanks,谢谢,

swati斯瓦蒂

Symbols '?', '&' and '=' are treated as argument and value separators in URL, so try to encode your XML first.符号 '?'、'&' 和 '=' 在 URL 中被视为参数和值分隔符,因此请先尝试对 XML 进行编码。

String xmlRequest = "url/query.do?Request=" + URLEncoder.encode("your xml", "UTF-8");
URL url = new URL(xmlRequest);
...

Browser performs URL encoding for every request.浏览器对每个请求执行 URL 编码。 That's why it works.这就是它起作用的原因。

This question is related to this one .这个问题与这个有关。

The error code 505 means the following:错误代码 505 的含义如下:

The server does not support, or refuses to support, the HTTP protocol version that was used in the request message.服务器不支持或拒绝支持请求消息中使用的 HTTP 协议版本。 The server is indicating that it is unable or unwilling to complete the request using the same major version as the client.服务器表明它无法或不愿意使用与客户端相同的主要版本完成请求。

its advisable to use apache HTTP client library (it has many features which can be used in your case).建议使用 apache HTTP 客户端库(它具有许多可用于您的情况的功能)。

you can also use Apache Tika api.您也可以使用 Apache Tika api。

consider the size of the URL being posted refer here ..考虑发布的 URL 的大小,请参阅此处..

using the apache HTTP client gets you the option to use the POST method instead of the GET method, this way you can post a fairly large file in the request.使用 apache HTTP 客户端可以选择使用 POST 方法而不是 GET 方法,这样您就可以在请求中发布相当大的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM