Java:如何创建SOAP请求?

时间:2012-08-16 07:23:09

标签: java soap

我有一个wsdl和xsd文件,并希望在python上创建对Web服务器的SOAP请求。我之前从未使用肥皂,所以我的问题可能很简单,但我花了四个小时却找不到解决办法。

我尝试了两种方法:Android上的低级请求和KSOAP2。

WSDL

    <wsdl:message name="<some request>">
    <wsdl:part element="txh:<some request>" name="parameters"/>
</wsdl:message>
<wsdl:message name="<some response>">
    <wsdl:part element="txh:<some response>" name="parameters"/>
</wsdl:message>

XSD

    <xs:element name=""<some request>">
    <xs:annotation>
        <xs:documentation>"<text>"</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element name="mode" type="response-mode"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="<some response>">
    <xs:complexType>
        <xs:annotation>
            <xs:documentation>"<text>"</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element ref="<text>"/>
            <xs:element name="<another text>" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="<name a>" type="xs:int"/>
                        <xs:element name="<name b>" type="xs:int"/>
                        <xs:element name="<name c>" type="xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Android中的代码:

private final static String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                         "<SOAP-ENV:Envelope " +
                         "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
                         "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" " +
                         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
                        "<SOAP-ENV:Header>" +
                        "</SOAP-ENV:Header>" +
                        "<SOAP-ENV:Body xmlns:ns1=\"<namespace>\">" +
                        "    <xs:element name=\"<request>\"> " +
                        "<xs:annotation>" +
                            "<xs:documentation>"<text>"</xs:documentation>" +
                        "</xs:annotation>" +
                        "<xs:complexType>" +
                            "<xs:sequence>" +
                                "<xs:element name=\""<text>"\" type=\"response-mode\"/>" +
                            "</xs:sequence>" +
                        "</xs:complexType>" +
                    "</xs:element>" +
                        "</SOAP-ENV:Body>" +
                        "</SOAP-ENV:Envelope>";



 public Entity execute(final String body) {
    Log.d(TAG, "Start request ");
    Entity result = new Entity();
    AndroidHttpClient client = AndroidHttpClient.newInstance(TAG);
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 10000);
    HttpConnectionParams.setSoTimeout(params, 15000);
    HttpProtocolParams.setUseExpectContinue(params, true);

    HttpPost post = new HttpPost(url);
    post.setParams(params);
    post.setHeader("soapaction", NAMESPACE.concat("/").concat(METHOD));
    post.setHeader("Content-Type", "text/xml; charset-utf8");
    try {
        String request = createRequest(xml);
        HttpEntity entityToRequest = new StringEntity(request);
        post.setEntity(entityToRequest);
        Log.d(TAG, post.toString());

        HttpResponse response = client.execute(post);
        final int status = response.getStatusLine().getStatusCode();
        if (HttpStatus.SC_OK == status) {
            HttpEntity entity = response.getEntity();
            String str = EntityUtils.toString(entity);
            Log.d(TAG, str);
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

在此请求中,我获得了500个服务器代码,因此我认为我的请求中存在问题。你觉得它形成得好吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 soapUI和类似工具可以帮助您理解肥皂请求的结构。