我正在执行以下java代码,使用java将XML请求发送到webservice URL。我查看了这个链接来创建我的脚本。 作为反应,我正在失败。我做了一个XML字符串的打印。 XML正确形成,没有任何问题。 我需要知道我哪里出错了,此时任何指导都会很棒。
import java.net.*;
import java.io.*;
import java.security.*;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLSocket;
import sun.net.www.http.HttpClient;
public class Sample {
public static void main(String[] args) throws Exception {
excutePost();
}
public static String excutePost() throws Exception {
URL url;
HttpURLConnection connection = null;
try {
//Create connection
String targetURL="https://XXXXXX.html";
System.out.println(targetURL);
String urlParameters="<?xml version=\"1.0\" encoding=\"iso-8859-1\"? >\r\n<request>\r\n <control>\r\n <senderid>XXXX</senderid>\r\n <password>XXXX</password>\r\n <controlid>ControlIdHere</controlid>\r\n <uniqueid>false</uniqueid>\r\n <dtdversion>3.0</dtdversion>\r\n </control>\r\n <operation>\r\n <authentication>\r\n <login>\r\n <userid>XXX</userid>\r\n <companyid>XXXX</companyid>\r\n <password>XXXX</password>\r\n </login>\r\n </authentication>\r\n\r\n<content>\r\n <function controlid=\"testControlId\">\r\n <readByQuery>\r\n <object>CUSTOMER</object>\r\n <query/>\r\n </readByQuery>\r\n </function>\r\n</content>\r\n </operation>\r\n</request>";
System.out.println(urlParameters);
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/xml");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
System.out.println(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}