我需要向Web服务发送xml请求并接受来自该Web服务的xml响应。
以下是代码:
public static void main(String[] args) throws IOException {
String url ="http://XX.XXX.X.XX:80/test";
String urlParameters = "";
StringBuilder test = new StringBuilder(768);
test.append("<?xml version='1.0'?>\n"+
"<!DOCTYPE COMMAND PUBLIC '-//Ocam//DTD XML Command 1.0//EN' 'xml/command.dtd'>\n"+
"<COMMAND>\n"+
"<TYPE>EXUSRBALREQ</TYPE>\n"+
"<DATE>14-03-17</DATE>\n"+
"<EXTNWCODE>MD</EXTNWCODE>\n"+
"<MSISDN>57625960</MSISDN>\n"+
"<PIN>47565</PIN>\n"+
"<LOGINID></LOGINID>\n"+
"<PASSWORD></PASSWORD>\n"+
"<EXTCODE>AD10001</EXTCODE>\n"+
"<EXTREFNUM>12345</EXTREFNUM>\n"+
"</COMMAND>\n");
System.out.println(test);
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content_Type", "application/xml");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Sending post on the URL"+url);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream(),"UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(responseCode);
System.out.println(response);
}
每当我运行应用程序时,我都会获得200状态,但下面是我得到的响应
响应:
Sending post on the URLhttp://XXX.XXX.X.XX:80/test
200
mclass^2&pid^61:6002:Your request is invalid. Please call 8900
有人可以帮助我。
此致
Amit Gupta
答案 0 :(得分:1)
您的代码汇总要在名为StringBuilder
的{{1}}中发送的XML。
但是,您实际上从未发送test
的内容,因此服务器不接收XML也就不足为奇了。请解决这个问题。
请参阅示例Sending HTTP POST Request In Java,了解如何使用Java发送POST请求。