我正在尝试将包含 xml 数据的调用发送到特定的 Web 服务,但收到响应代码 500。我正在努力解决此问题。任何意见将不胜感激。
String wsURL = "https://wd2-impl-services1.workday.com/ccx/service/Payteam/v10.2?wsdl";
URL url = null;
URLConnection 连接= null;
HttpURLConnection httpConn = null;
字符串responseString = null;
字符串outputString =“”;
OutputStream out = null;
InputStreamReader isr = null;
BufferedReader in = null;``
String xmlInput =
“XML文档”
try
{
url = new URL(wsURL);
connection = url.openConnection();
httpConn = (HttpURLConnection) 连接;`
byte[] buffer = new byte[xmlInput.length()];``
缓冲区 = xmlInput.getBytes();`
String SOAPAction = "";
//设置相应的HTTP参数\属性。
httpConn.setRequestProperty("Content-Length", String.valueOf(buffer.length));
httpConn .setRequestProperty("Content-Type","text/xml; charset-utf-8");
httpConn.setRequestProperty("用户名","用户名");
httpConn.setRequestProperty("密码", "密码”);`
httpConn.setRequestProperty("SOAPAction",SOAPAction);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
out = httpConn.getOutputStream();
out.write(buffer);
out.close();
//Read Response
isr = new InputStreamReader(httpConn.getInputStream());
in = new BufferedReader(isr);
while ((responseString = in.readLine()) != null)
{
outputString = outputString + responseString;
}
System.out.println(outputString);
System.out.println("");
Document document = parseXmlFile(outputString);
}``
catch(异常e){
e.printStackTrace();
}`
}
private static Document parseXmlFile(String in) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(in));
return db.parse(is);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
您需要使用 JAXB 根据上面链接到的 wsdl 文件生成客户端。然后使用所述客户端与 SOAP Web 服务进行通信。
这已经在网上描述了一百万次,所以我将在这里链接到一个很好的解释: