0
<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。看来我必须将我的类扩展到另一个包含这三种方法的类。可能是哪个班?我可能会丢失哪些罐子?调用上述方法并传递所需参数的简单 main 方法会起作用吗?

4

1 回答 1

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");
        }
    }

在这种情况下使用文件,但它可以是一个简单的字符串。您必须从该字符串中创建一个肥皂消息。

于 2012-12-20T17:17:12.250 回答