【问题标题】:How to post xml over HTTPS in java?如何在 Java 中通过 HTTPS 发布 xml?
【发布时间】:2012-11-25 23:45:19
【问题描述】:
<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;

header 我被告知是我发布的 XML。我不确定内容类型,有人建议它可能是 XML。项目类型应该是使用 Tomcat 服务器的动态 Web 项目。我还被告知要获取 org.apache.commons.httpclient 库。

我一直在尝试将各个部分拼凑在一起,但我失败了。我收到错误:getHttpClient(),说该方法无法解析。日志相同,无法解决 InvalidRequestException。看来我必须将我的类扩展到另一个包含这三种方法的类。可能是哪个班?我可能会丢失哪些罐子?调用上述方法并传递所需参数的简单 main 方法可以工作吗?

【问题讨论】:

  • 您似乎正在尝试编写自己的 SOAP 客户端 API。除了正文有效负载之外,SOAP 还具有 HTTP 标头要求。有关一些文档链接,请参阅 here。但请考虑使用 Java SOAP 库(例如 JAX-RPC;JAX-WS。)
  • 如果你能告诉我一些代码,那就太好了。
  • @Nomain Arain - question 10671494 可以帮助你。但如果没有别的,我建议使用JAX-WS 客户端API 进行错误处理。如果您想使用手动 HTTP 代码正确处理 SOAP 消息,您别无选择,只能阅读规范并实现它。

标签: java xml web-services


【解决方案1】:
/*
     * 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");
        }
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    相关资源
    最近更新 更多