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 が解決できませんでした。これらの 3 つのメソッドを含む別のクラスにクラスを拡張する必要があるようです。それはどのクラスでしょうか?どの瓶が欠けている可能性がありますか? 上記のメソッドを呼び出して必要な引数を渡す単純なメイン メソッドは機能しますか?

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

この場合はfileを使用しますが、単純な文字列にすることもできます。その文字列からsoapmessageを作成する必要があります。

于 2012-12-20T17:17:12.250 に答える