-->

How to consume SAP SOAP Webservices, with ksoap2,

2019-09-13 09:47发布

问题:

I want to consume SAP webservices in my android app. The webservices are working as intended in SOAPUI (generated requests from the sap wsdl). I have tried using ksoap2 . When using ksoap2 I get "Webservice processing error" as response. I have exhausted all results from google/stackoverflow/sap community network. I have tried the request sent from ksoap2 in SOAPUI, but I am unable to pin down the crucial difference between it and the working (SOAPUI generated) request. Please guide me.

ksoap2 code:

public class SoapRetail extends Activity {

private static final String NAMESPACE   = "urn:sap-com:document:sap:rfc:functions";
private static final String SERVICE_URL = "http://icanhazcheezburger.com/sap/bc/srt/rfc/sap/zretail_exec_topproducts_webs/100/zretail_exec_topproducts_webs/zretail_exec_topproducts_webs";
private static final String METHOD_NAME = "ZRetail_Exec_TopProducts_WebS/ZRFC_TOPCUSTOMER_NEWRequest";
private static final String SOAP_ACTION = "urn:sap-com:document:sap:rfc:functions/ZRetail_Exec_TopProducts_WebS/ZRFC_TOPCUSTOMER_NEWRequest";

private static final String USERNAME    = "Alex";
private static final String PASSWORD    = "hunter2";
private static final int    TIMEOUT     = 10000;

@Override
protected void onStart() {
    super.onStart();

    TextView txt = (TextView) findViewById(R.id.textView1);
    txt.setTextSize(10);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapObject so_etTopCustomers = new SoapObject(NAMESPACE,
            "ET_TOPCUSTOMERS");
    SoapObject so_item = new SoapObject(NAMESPACE, "item");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER10); //Also tried VER11 and VER12
    envelope.dotNet = false;

    envelope.setOutputSoapObject(request);

    PropertyInfo kunnr = new PropertyInfo();
    kunnr.setName("KUNNR");
    // kunnr.setValue("");              // Tried with and without these 
    // kunnr.setType(String.class);
    so_item.addProperty(kunnr);

    PropertyInfo name1 = new PropertyInfo();
    name1.setName("NAME1");
    // name1.setValue("");
    // name1.setType(String.class);
    so_item.addProperty(name1);

    PropertyInfo custnum = new PropertyInfo();
    custnum.setName("CUSTNUM");
    // custnum.setValue("");
    // custnum.setType(String.class);
    so_item.addProperty(custnum);

    PropertyInfo netwr = new PropertyInfo();
    netwr.setName("NETWR");
    // netwr.setValue("");
    // netwr.setType(String.class);
    so_item.addProperty(netwr);

    so_etTopCustomers.addSoapObject(so_item);

    request.addSoapObject(so_etTopCustomers);

    PropertyInfo i_toplist = new PropertyInfo();
    i_toplist.setName("I_TOPCUST");
    // i_toplist.setValue("10");
    // i_toplist.setType(String.class);
    request.addProperty(i_toplist);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVICE_URL,
            TIMEOUT);
    androidHttpTransport.debug = true;

    try {
        List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
        headerList.add(new HeaderProperty("Authorization", "Basic "
                + org.kobjects.base64.Base64.encode(USERNAME+":"+PASSWORD
                        .getBytes())));
        headerList
                .add(new HeaderProperty(
                        "SOAPAction",
                        SOAP_ACTION));
        Log.i("Info", "Now trying androidHttpTransport.call()...");
        androidHttpTransport.call(SOAP_ACTION, envelope, headerList);
        SoapObject response = (SoapObject) envelope.getResponse();
    } catch (IOException e) {
        Log.e("IOException", "androidHttpTransport.call()");
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        Log.e("XmlPullParserException", "androidHttpTransport.call()");
        e.printStackTrace();
    }

    txt.setText("Request sent:\n\n" + androidHttpTransport.requestDump
            + "\n\nResponse recieved:\n\n"
            + androidHttpTransport.responseDump);
    Log.d("Request sent", androidHttpTransport.requestDump);
    Log.d("Response recieved", androidHttpTransport.responseDump);

    SoapObject result = null;
    try {
        Log.i("Info", "Now trying envelope.getResponse()...");
        result = (SoapObject) envelope.getResponse();
    } catch (SoapFault e) {
        Log.e("SoapFault", "envelope.getResponse()");
        e.printStackTrace();
    } catch (Exception e) {
        Log.e("Exception", "envelope.getResponse()");
        e.printStackTrace();
    }
}
}

Corresponding Logcat for ksoap2 approach:

10-31 10:05:46.670: I/Info(2045): Now trying androidHttpTransport.call()...
10-31 10:05:47.769: E/IOException(2045): androidHttpTransport.call()
10-31 10:05:47.769: W/System.err(2045): SoapFault - faultcode: 'soap-env:Server' faultstring: 'Web service processing error; more details in the web service error log on provider side (UTC timestamp 20121031043547; Transaction ID 701423E22D06F17F9A51001C23D23A48)' faultactor: 'null' detail: org.kxml2.kdom.Node@4123f288
10-31 10:05:47.779: W/System.err(2045):     at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:141)
10-31 10:05:47.790: W/System.err(2045):     at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
10-31 10:05:47.790: W/System.err(2045):     at org.ksoap2.transport.Transport.parseResponse(Transport.java:100)
10-31 10:05:47.790: W/System.err(2045):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:214)
10-31 10:05:47.809: W/System.err(2045):     at play.ground.soap.SoapRetail.onStart(SoapRetail.java:136)
10-31 10:05:47.809: W/System.err(2045):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
10-31 10:05:47.809: W/System.err(2045):     at android.app.Activity.performStart(Activity.java:5018)
10-31 10:05:47.819: W/System.err(2045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
10-31 10:05:47.819: W/System.err(2045):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-31 10:05:47.850: W/System.err(2045):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-31 10:05:47.850: W/System.err(2045):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-31 10:05:47.850: W/System.err(2045):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 10:05:47.850: W/System.err(2045):     at android.os.Looper.loop(Looper.java:137)
10-31 10:05:47.850: W/System.err(2045):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-31 10:05:47.859: W/System.err(2045):     at java.lang.reflect.Method.invokeNative(Native Method)
10-31 10:05:47.859: W/System.err(2045):     at java.lang.reflect.Method.invoke(Method.java:511)
10-31 10:05:47.859: W/System.err(2045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-31 10:05:47.859: W/System.err(2045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 10:05:47.859: W/System.err(2045):     at dalvik.system.NativeStart.main(Native Method)
10-31 10:05:47.949: D/Request sent(2045): <v:Envelope xmlns:i="http://www.w3.org/1999/XMLSchema-instance" xmlns:d="http://www.w3.org/1999/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><n0:ZRetail_Exec_TopProducts_WebS/ZRFC_TOPCUSTOMER_NEWRequest id="o0" c:root="1" xmlns:n0="urn:sap-com:document:sap:rfc:functions"><n0:ET_TOPCUSTOMERS i:type="n0:ET_TOPCUSTOMERS"><n0:item i:type="n0:item"><KUNNR i:null="true" /><NAME1 i:null="true" /><CUSTNUM i:null="true" /><NETWR i:null="true" /></n0:item></n0:ET_TOPCUSTOMERS><I_TOPCUST i:null="true" /></n0:ZRetail_Exec_TopProducts_WebS/ZRFC_TOPCUSTOMER_NEWRequest></v:Body></v:Envelope>

10-31 10:05:47.966: D/Response recieved(2045): <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Header/><soap-env:Body><soap-env:Fault><faultcode>soap-env:Server</faultcode><faultstring xml:lang="en">Web service processing error; more details in the web service error log on provider side (UTC timestamp 20121031043547; Transaction ID 701423E22D06F17F9A51001C23D23A48)</faultstring><detail/></soap-env:Fault></soap-env:Body></soap-env:Envelope>

SOAPUI generated request (that works in SOAPUI):

POST http://www.icanhazcheezeburger.com/sap/bc/srt/rfc/sap/zrfc_topcustomer_webs/100/zrfc_topcustomer_webs/zrfc_topcustomer_webs HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "urn:sap-com:document:sap:rfc:functions:ZRFC_TOPCUSTOMER_webs:ZRFC_TOPCUSTOMER_NEWRequest"
Authorization: Basic <encoded"Alex:hunter2">=
Content-Length: 596
Host: icanhazcheezeburger.com:22
Proxy-Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:rfc:functions">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:ZRFC_TOPCUSTOMER_NEW>
         <!--Optional:-->
         <ET_TOPCUSTOMERS>
            <!--Zero or more repetitions:-->
            <item>
               <KUNNR/>
               <NAME1/>
               <CUSTNUM/>
               <NETWR/>
            </item>
         </ET_TOPCUSTOMERS>
         <!--Optional:-->
         <I_TOPCUST>10</I_TOPCUST>
      </urn:ZRFC_TOPCUSTOMER_NEW>
   </soapenv:Body>
</soapenv:Envelope>

UPDATE:

I am able consume webservices, using a different approach, i.e. without using ksoap2. I copied the SOAP UI generated response and sent it in a HTTP POST request. I had to add SOAPAction and Auth headers. If anyone needs the code let me know. Thanks all. I leave the question open since I am not able to do so using ksoap2.