发送SOAP请求

时间:2011-08-02 09:14:49

标签: android soap http-post

在我的应用程序中,我想向某个URL发送SOAP请求,要发送的SOAP请求如下所示

POST /TelLink/WrenchENTService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WrenchGlobal/GetToDoList"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetToDoList xmlns="http://WrenchGlobal/">
    <viPeriod>int</viPeriod>
    <vsUserID>string</vsUserID>
  </GetToDoList>
</soap:Body>
</soap:Envelope>

在上面的代码中,我将不得不用实际值替换“int”和“string”,它将在服务器上调用GetToDoList。我的问题是,我不知道如何将此请求发送到服务器? (使用httppost)任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

int viPeriod;
String vsUserID;
String NAMESPACE = "http://WrenchGlobal/";
String SOAP_ACTION = "http://WrenchGlobal/GetToDoList";
String METHOD_NAME = "GetToDoList";

String result = null;
Object resultRequestSOAP = null;

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

request.addProperty("viPeriod", viPeriod);
request.addProperty("vsUserID", vsUserID);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

envelope.dotNet = true;
envelope.setOutputSoapObject(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;

try {
    androidHttpTransport.call(SOAP_ACTION, envelope);

    String requestDumpString = androidHttpTransport.requestDump;
    System.out.println("requestDump : " + requestDumpString);

    resultRequestSOAP = envelope.getResponse(); // Output received
    result = resultRequestSOAP.toString(); // Result string

    System.out.println("OUTPUT : " + result);
} catch (Exception e) {
    e.printStackTrace();
}

希望这会有所帮助。

答案 1 :(得分:0)

IntentService HttpPost可能就是您想要的。 Google提供了大量的代码段; here只是其中之一。