我有一個wsdl和xsd文件,並希望在python上創建SOAP請求到web服務器。我從來沒有用過肥皂,所以我的問題可能很簡單,但我花了四個小時,並沒有找到解決方案。Java:如何創建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>
代碼在安卓
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服務器代碼,所以我想在我的請求,問題。你認爲它形成良好嗎?
您可以看看Apache Axis2框架,該框架爲基於SOAP的Web服務提供了生成器和大量內容。 – 2012-08-16 07:26:39