我需要调用具有 SOAP 信封的 WCF 服务,如下所示:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">SessionStart</a:Action>
<a:MessageID>urn:uuid:eca7579d-5581-4989-984d-0ead2e670194</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">http://[WCF Service URL].svc</a:To>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<XMLMessage xmlns="http://[Client Website URL]/MobileInterface">
<SessionManagementRequest>
<IMEI>123456</IMEI>
<OfficerNumber>AA</OfficerNumber>
<Password>AA</Password>
<Status>Setup</Status>
<UserName>AA</UserName>
<XSDVersion>1</XSDVersion>
</SessionManagementRequest>
</XMLMessage>
</s:Body>
</s:Envelope>
我正在努力的部分是获取 XMLMessage SOAPObject 的值。以下是我的代码示例:
public static String requestSessionManagement() throws Exception
{
int i;
int timeout;
PropertyInfo pi;
HttpTransportSE transport;
String soap_action;
String method = "GetData";
String result = "";
timeout = ServerInterface.TIMEOUT;
soap_action = _soap_namespace + method;
// Check array to know if custom timeout is needed
for (i = 0;i < func_timeouts.length;i++)
{
if (func_timeouts[i][0].compareTo(method) == 0)
{
timeout = Integer.parseInt(func_timeouts[i][1]);
}
}
Log.i("[ServerInterface]", "sending request to "+_server_url+": "+_soap_namespace+method);
Log.i("[ServerInterface]", "timeout: "+timeout);
// Build request
SoapObject request = new SoapObject(_soap_namespace, "XMLMessage");
MapRequestSessionManagement param1 = new MapRequestSessionManagement();
param1.imei = "123456";
param1.officer_number = "1029384";
param1.password = "test1!";
param1.status = "Setup";
param1.username = "MichaelJS";
param1.xsd_version = "1";
pi = new PropertyInfo();
pi.setName("SessionManagementRequest");
pi.setValue(param1);
pi.setType(MapRequestSessionManagement.MAPREQUESTSESSIONMANAGEMENT_CLASS);
request.addProperty(pi);
// Build envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);// Soap version must match server implementation
//Prepare header
envelope.headerOut = new Element[4];
envelope.headerOut[0] = ServerSyncHelper.buildActionHeader("SessionStart");
envelope.headerOut[1] = ServerSyncHelper.buildMessageIDHeader();
envelope.headerOut[2] = ServerSyncHelper.buildReplyToHeader("http://www.w3.org/2005/08/addressing/anonymous");
envelope.headerOut[3] = ServerSyncHelper.buildToHeader(_server_url);
envelope.dotNet = true; // Must set this if the server is implemented on the dotnet framework
envelope.implicitTypes = true;
envelope.setOutputSoapObject(request);
// Set http transport
transport = new HttpTransportSE(_server_url, timeout);
transport.debug = true; //this is optional, use it if you don't want to use a packet sniffer to check what the sent message was (httpTransport.requestDump, httpTransport.responseDump)
try
{
transport.call(soap_action, envelope);
Log.i("[ServerInterface]", "request dump "+transport.requestDump);
Log.i("[ServerInterface]", "response dump "+transport.responseDump);
Log.i("[ServerInterface]", "response "+envelope.bodyIn);
String response = (String)envelope.getResponse();
result = ""+response;
}
catch (Exception e)
{
Log.i("[ServerInterface]", "Exception part");
Log.i("[ServerInterface]", "request dump "+transport.requestDump);
Log.i("[ServerInterface]", "response dump "+transport.responseDump);
Log.e("[ServerInterface]", "Error with soap transmition "+e);
e.printStackTrace();
}
return result;
}
当我运行上面的代码时,我得到以下结果,这是正确的。但是,我不确定如何根据服务器的要求进行格式化:
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.w3.org/2001/12/soap-encoding" xmlns:v="http://www.w3.org/2001/12/soap-envelope">
<v:Header>
<Action v:mustUnderstand="1" xmlns="http://www.w3.org/2005/08/addressing">SessionStart</Action>
<MessageID xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:49bd428d-188a-4035-8d8f-45bcb42b37ec</MessageID>
<ReplyTo xmlns="http://www.w3.org/2005/08/addressing">
<Address xmlns="http://www.w3.org/2005/08/addressing">http://www.w3.org/2005/08/addressing/anonymous</Address>
</ReplyTo>
<To v:mustUnderstand="1" xmlns="http://www.w3.org/2005/08/addressing">http://41.134.120.203/MobileInterfaceService/SessionManagementService.svc</To>
</v:Header>
<v:Body>
<XMLMessage xmlns="http://www.syntell.com/MobileInterface/" id="o0" c:root="1">
<SessionManagementRequest>
<IMEI>123456</IMEI>
<OfficerNumber>1029384</OfficerNumber>
<Password>test1!</Password>
<Status>Setup</Status>
<UserName>MichaelJS</UserName>
<XSDVersion>1</XSDVersion>
</SessionManagementRequest>
</XMLMessage>
</v:Body>
</v:Envelope>
似乎他们已经对 Server 方法进行了编码,以接受包含 SessionManagementRequest 对象的 XML 结构的字符串参数。