<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"^M
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"^M
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"^M
xmlns:xsd="http://www.w3.org/2001/XMLSchema"^M
xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">^M
<env:Header>^M
<wsse:Security>^M
<wsse:UsernameToken>^M
<wsse:Username>user</wsse:Username>^M
<wsse:Password>pass</wsse:Password>^M
</wsse:UsernameToken>^M
</wsse:Security> ^M
</env:Header>
我被告知将此xml发布到:https://www.abc.com 我也有以下方法:
public int sendPostRequest(String url, String content, String contentType)
throws Exception {
PostMethod post = new PostMethod(url);
post.setRequestEntity(new StringRequestEntity(content, contentType,
null));
boolean success = false;
String responseBody = null;
int statusCode;
try {
getHttpClient().executeMethod(post);
success = true;
statusCode = post.getStatusCode();
responseBody = post.getResponseBodyAsString();
} catch (Exception ex) {
log.error("Marhsalling exception : " + ex.getMessage());
throw new InvalidRequestException("Marhsalling exception :"
+ ex.getMessage());
} finally {
post.releaseConnection();
}
if ((statusCode != HttpStatus.SC_OK) &&
(statusCode != HttpStatus.SC_NO_CONTENT)) {
String error = "Got Bad Http Status - <" + statusCode
+ "> Info : " + responseBody;
log.error(error);
throw new InvalidRequestException(error);
} else {
log.debug("Success - " + responseBody);
}
return statusCode;
}
private String footer = "</env:Envelope>";
private String message = "<InstallService><NewAccount></NewAccount></InstallService>";
private String payload = header + message + footer;
标题我被告知是我发布的XML。我不确定内容类型,有人建议它可以是XML。项目类型应该是使用Tomcat服务器的动态Web项目。我还被告知要抓住org.apache.commons.httpclient库。
我一直试图把各个部分放在一起,但我没有。我收到错误:getHttpClient(),说该方法无法解决。对于日志是相同的,并且无法解析InvalidRequestException。似乎我必须将我的类扩展到另一个包含这三种方法的类。可能是哪一堂课?我可以丢失哪些罐子?调用上述方法并传递所需参数的简单主方法可以工作吗?
答案 0 :(得分:0)
/*
* soapXMLtoEndpoint sends the soapXMLFileLocation to the endpointURL
*/
public void soapXMLtoEndpoint(String endpointURL, String soapXMLFileLocation) throws SOAPException {
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = connection.call(xmlStringToSOAPMessage(soapXMLFileLocation), endpointURL);
connection.close();
SOAPBody responseBody = response.getSOAPBody();
SOAPBodyElement responseElement = (SOAPBodyElement) responseBody.getChildElements().next();
SOAPElement returnElement = (SOAPElement) responseElement.getChildElements().next();
if (responseBody.getFault() != null) {
System.out.println("fault != null");
System.out.println(returnElement.getValue() + " " + responseBody.getFault().getFaultString());
} else {
serverResponse = returnElement.getValue();
System.out.println(serverResponse);
System.out.println("\nfault == null, got the response properly.\n");
}
}
在这种情况下使用文件,但它可以是一个简单的字符串。您必须从该字符串中创建一个soapmessage。