我需要手动访问 .Net SOAP 服务。所有的进口商都有它的 WSDL 问题,所以我只是手动创建 XML 消息,使用 HttpURLConnection 连接,然后解析结果。我已经将 Http/SOAP 调用包装到一个函数中,该函数应该将结果作为字符串返回。这是我所拥有的:
//passed in values: urlAddress, soapAction, soapDocument
URL u = new URL(urlAddress);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("SOAPAction", soapAction);
connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
connection.setRequestProperty("Accept","[star]/[star]");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
//helper function that gets a string from a dom Document
String xmldata = XmlUtils.GetDocumentXml(soapDocument);
wout.write(xmldata);
wout.flush();
wout.close();
// Response
int responseCode = connection.getResponseCode();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseString = "";
String outputString = "";
//Write the SOAP message response to a String.
while ((responseString = rd.readLine()) != null) {
outputString = outputString + responseString;
}
return outputString;
我的问题是BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
我得到一个带有我正在使用的地址的“java.io.FileNotFoundException”(即urlAddress)。如果我将该地址粘贴到浏览器中,它会很好地打开 Soap 服务网页(地址是http://protectpaytest.propay.com/API/SPS.svc)。根据我的阅读,FileNotFoundException 是 HttpURLConnection 返回 400+ 错误消息。我添加了行 getResponseCode() 只是为了查看确切的代码是什么,它是 404。我从其他一些页面添加了 User-Agent 和 Accept 标头,说它们是必需的,但我仍然得到 404。
我还缺少其他标题吗?我还需要做什么才能让这个调用工作(因为它在浏览器中工作)?
-shnar