在 JavaScript 中,我们有以下实现:
function sendRMSRequest(xmlString, url){
if(window.XMLHttpRequest){
xmlRequest = new XMLHttpRequest();
}
xmlRequest.open("POST", url, false);
xmlRequest.onreadystatechange = function() {processRequest();};
xmlRequest.send(xmlString);
}
xmlString 具有以下字符串:
<TestRequest xmlns='http://???.???.????' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'></TestRequest>
我想将此功能移至服务器端,但我不知道如何在 java 中实现此功能。
我目前有以下java代码:
public static void main(String[] args) throws IOException {
String endPoint = "http://????.????/service";
String xml = "<TestRequest xmlns='http://???.???.????' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'></TestRequest>";
System.out.println(xml);
URL url = new URL(endPoint);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoInput(true);
httpCon.setDoOutput(true);
Authenticator.setDefault(new MyAuthenticator());
httpCon.setReadTimeout(0);
httpCon.setConnectTimeout(0);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type", "text/plain");
httpCon.setRequestProperty("charset", "utf-8");
httpCon.setRequestProperty("", xml);
PrintWriter pw = new PrintWriter(httpCon.getOutputStream());
pw.write(xml);
pw.flush();
pw.close();
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
System.out.println(httpCon.getContentType());
String returnString = null;
StringBuffer sb = null;
BufferedInputStream in;
// set up httpConn code not included same as previous
in = new BufferedInputStream(httpCon.getInputStream());
int x = 0;
sb = new StringBuffer();
while ((x = in.read()) != -1) {
sb.append((char) x);
}
in.close();
in = null;
if (httpCon != null) {
httpCon.disconnect();
}
returnString = sb.toString();
System.out.println("Return String = " + returnString);
}
运行上面的代码时出现以下错误:
org.xml.sax.SAXParseException: Premature end of file.
任何帮助,将不胜感激。
谢谢丹尼尔