Java将xml属性放在HTTP请求的“请求版本”部分
我能够使用浏览器发送以下请求并接收正确的响应,但当我使用以下代码发送请求时,服务器返回错误 我怀疑问题在于Java将xml属性放在HTTP请求的请求版本部分Java将xml属性放在HTTP请求的“请求版本”部分,java,xml,httpurlconnection,wireshark,Java,Xml,Httpurlconnection,Wireshark,我能够使用浏览器发送以下请求并接收正确的响应,但当我使用以下代码发送请求时,服务器返回错误 我怀疑问题在于Java将xml属性放在HTTP请求的请求版本部分 String QueryString = "http://xml.example.com/service?" + "<List>" + "<Credentials username=\"example\" password=\"example1\" remoteIp=\"X.
String QueryString = "http://xml.example.com/service?"
+ "<List>"
+ "<Credentials username=\"example\" password=\"example1\" remoteIp=\"X.X.X.X\"/>"
+ "<Id>10202</Id>"
+ "</List>";
try {
URL page = new URL(QueryString);
StringBuffer text = new StringBuffer();
HttpURLConnection conn = (HttpURLConnection) page.openConnection();
conn.connect();
InputStreamReader in = new InputStreamReader(
(InputStream) conn.getContent());
我将附和对问题的评论,说这对于您正在与之通信的服务器来说是一个相当糟糕的表示选择,但是如果这是服务提供商提供的唯一选项,那么 如果服务器需要URI编码的查询字符串,则可以使用java.net.URI的多参数构造函数为您处理必要的转义:
URI uri = new URI("http", "xml.example.com", "/service",
"<List>"
+ "<Credentials username=\"example\" password=\"example1\" remoteIp=\"X.X.X.X\"/>"
+ "<Id>10202</Id>"
+ "</List>", null);
URL page = uri.toURL();
如果您想在请求主体中发布数据,而不是在URL中提供数据,那么为了清晰起见,您需要类似于此异常处理的内容
URL page = new URL("http://xml.example.com/service");
HttpURLConnection connection = page.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml"); // or text/xml
connection.setDoOutput(true); // need to call this before you can getOutputStream
// write the XML as UTF-8 (which is what an XML parser will expect as you're
// sending it without an <?xml declaration that says anything different)
Writer writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write("<List>"
+ "<Credentials username=\"example\" password=\"example1\" remoteIp=\"X.X.X.X\"/>"
+ "<Id>10202</Id>"
+ "</List>");
writer.close();
这非常难看,让我强调一下,以这样的查询字符串发送数据非常难看。改为使用POST请求的主体。同样针对您的问题-问题可能是URI编码。浏览器在用户没有注意到的情况下自动进行编码。在Java代码中,您需要手动执行此操作或在语义上构造URL对象,甚至看起来您正在尝试这样做身份验证。URL被转储到访问日志中,因此您将在这些文件中公开用户凭据。@PavelHoll您能否帮助我将其放入正文中?请通过JRE组件发送帖子可能有点烦人。检查Apache HTTP组件库。谢谢,它可以作为所需的URI编码查询字符串工作。实际上,服务器需要是https,我使用http的原因是为了能够使请求和响应可读,并且我对将请求放在post正文中非常感兴趣,你能帮我吗?我应该在另一个问题中问这个问题吗?@Alex我已经添加了一个如何做的示例,基本上与您在中展示的相同。
URI uri = new URI("http", "xml.example.com", "/service",
"<List>"
+ "<Credentials username=\"example\" password=\"example1\" remoteIp=\"X.X.X.X\"/>"
+ "<Id>10202</Id>"
+ "</List>", null);
URL page = uri.toURL();
URL page = new URL("http://xml.example.com/service?"
+ URLEncoder.encode("<List>"
+ "<Credentials username=\"example\" password=\"example1\" remoteIp=\"X.X.X.X\"/>"
+ "<Id>10202</Id>"
+ "</List>", "UTF-8"));
URL page = new URL("http://xml.example.com/service");
HttpURLConnection connection = page.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml"); // or text/xml
connection.setDoOutput(true); // need to call this before you can getOutputStream
// write the XML as UTF-8 (which is what an XML parser will expect as you're
// sending it without an <?xml declaration that says anything different)
Writer writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write("<List>"
+ "<Credentials username=\"example\" password=\"example1\" remoteIp=\"X.X.X.X\"/>"
+ "<Id>10202</Id>"
+ "</List>");
writer.close();