Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

android - How to create proper soap envelope (request xml) in code while using KSoap2?

This is the soap request as obtained from SoapUi by feeding the wsdl.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://wsclient.xyz.com/types/">
   <soapenv:Header/>
   <soapenv:Body>
      <typ:loginserviceElement>
         <typ:username>test.test</typ:username>
         <typ:password>test123</typ:password>
      </typ:loginserviceElement>
   </soapenv:Body>
</soapenv:Envelope>

But the android code dumped (from logcat) the request as:

<?xml version="1.0" encoding= "UTF-8" ?>
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema"
 xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
        <n0:loginservice xmlns:n0="http://wsclient.xyz.com//">
            <username>test.test</username>
            <password>test1234</password>
        </n0:loginservice>
    </v:Body>
</v:Envelope>

My question is- Is both the xml request are same or should work interchangeably? If not how can I customize the request to match the one I get from SoapUi?

As mentioned in the reposnce I am getting:

SoapFault - faultcode: 'env:Client' faultstring: 'Caught exception while handling request: unrecognized operation: {http://wsclient.xyz.com//}loginservice' faultactor: 'null' detail: null

Response from SoapUi (what I am trying to achive in anddroid code):

  <env:Body>
      <ns0:loginserviceResponseElement>
         <ns0:result>
            <ns0:logintoken>181210281021ahash</ns0:logintoken>
            <ns0:hrmsid>0000002</ns0:hrmsid>
         </ns0:result>
      </ns0:loginserviceResponseElement>
   </env:Body>
</env:Envelope>

I have tried many answer as found on SO and other tutorials with out success.

I would be grateful if a reference to good link can be provided that some how clearly describes the different tags like v:Envelope or soapenv:Envelope, n0:loginservice or typ:loginserviceElement or type:loginserviceElement etc

Below is the android code for reference:

public class SoapRequests {

    private static final boolean DEBUG_SOAP_REQUEST_RESPONSE = true;    
    private static final String MAIN_REQUEST_URL = "http://abc.xyz.com/WSClient/WSServiceSoapHttpPort";
    private static final String NAMESPACE = "http://wsclient.xyz.com//";
    private static final String SOAP_ACTION = "http://wsclient.xyz.com//loginservice";
    private static String SESSION_ID;

    private final void testHttpResponse(HttpTransportSE ht) {
        ht.debug = DEBUG_SOAP_REQUEST_RESPONSE;
        if (DEBUG_SOAP_REQUEST_RESPONSE) {
            Log.v("SOAP RETURN", "Request XML:
" + ht.requestDump);
            Log.v("SOAP RETURN", "


Response XML:
" + ht.responseDump);
        }
    }

    public User getUserData(String name, String pwd){       
         User user = null;
         String methodname = "loginservice";

         SoapObject request = new SoapObject(NAMESPACE, methodname);         

         PropertyInfo userName =new PropertyInfo();
         userName.setName("username");
         userName.setValue(name);
         userName.setType(String.class);
         request.addProperty(userName);

         PropertyInfo password =new PropertyInfo();
         password.setName("password");
         password.setValue(pwd);
         password.setType(String.class);
         request.addProperty(password);

         SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
         HttpTransportSE ht = getHttpTransportSE();

         try {
             ht.call(SOAP_ACTION, envelope);
             testHttpResponse(ht);
             SoapPrimitive resultsString = (SoapPrimitive)envelope.getResponse();
             String data = resultsString.toString();
             Log.v("***********RESPONSE*******************", data);

         } catch (SocketTimeoutException t) {
             t.printStackTrace();
         } catch (IOException i) {
             i.printStackTrace();
         } catch (Exception q) {
             q.printStackTrace();
         }

         // some code to set user data
         ....
         return user;

    }

    private SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.implicitTypes = true;
        envelope.setAddAdornments(false);
        envelope.setOutputSoapObject(request);
        return envelope;
    }

    private final HttpTransportSE getHttpTransportSE() {
        HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY,MAIN_REQUEST_URL,60000);
        ht.debug = true;
        ht.setXmlVersionTag("<?xml version="1.0" encoding= "UTF-8" ?>");
        return ht;
    }
}

EDIT: RESPONSE from android code:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://wsclient.xyz.com/types/">
    <env:Body>
    <env:Fault>
        <faultcode>env:Client</faultcode>
        <faultstring>Caught exception while handling request: unrecognized operation: {http://wsclient.hrms.com//}loginservice</faultstring>
    </env:Fault>
    </env:Body>
</env:Envelope>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well finally I got it working after some more research and got the answers to the questions.

1) Though SoapUi genearted <soapenv:Envelope xmlns:soapenv="...." ....> type response xml and android code using Ksoap2 library genearted <v:Envelope xmlns:i="..." ...> type response xml have different looking tags, it is not significant in getting errors. Both are similar.

As mentioned on the answer to SO question, ksoap has hardcoded values for the namespace in SoapEnvelope.

2) The unrecognized operation exception was due to issue in the MAIN_REQUEST_URL and NAMESPACE. Knowing the proper value of url, namespace and the soap_action is bit tricky, at least for a beginner in this space.

The values of these fields can be set by looking at the request/response xml, wsdl and this nice pictorial example.

In my case, I had to change

MAIN_REQUEST_URL = "http://abc.xyz.com/WSClient/WSServiceSoapHttpPort";
NAMESPACE = "http://wsclient.xyz.com//";
SOAP_ACTION = "http://wsclient.xyz.com//loginservice";

to

MAIN_REQUEST_URL = "http://abc.xyz.com/WSClient/WSServiceSoapHttpPort?WSDL";
NAMESPACE = "http://wsclient.xyz.com/types/";
SOAP_ACTION = "http://wsclient.xyz.com//loginservice";

and also I had to change:

String methodname = "loginservice";

to

String methodname = "loginserviceElement";

as the request/response xml has this ( typ:loginserviceElement ) tag wrapping the properties/parameters.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...