使用KSoap2时如何在代码中创建适当的soap信封(请求xml)? [英] How to create proper soap envelope (request xml) in code while using KSoap2?

查看:21
本文介绍了使用KSoap2时如何在代码中创建适当的soap信封(请求xml)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是通过提供 wsdl 从 SoapUi 获得的soap请求.

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>

但是 android 代码(从 logcat)将请求转储为:

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>

我的问题是 - 两个 xml 请求是相同的还是应该互换工作?如果不是,我如何自定义请求以匹配我从 SoapUi 获得的请求?

正如我收到的回复中提到的:

As mentioned in the reposnce I am getting:

SoapFault - 故障代码:'env:Client' 故障字符串:'处理请求时捕获异常:无法识别的操作:{http://wsclient.xyz.com//}loginservice' faultactor: 'null' 详细信息:null

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

来自 SoapUi 的响应(我试图在 android 代码中实现的目标):

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>

我尝试了很多在 SO 和其他教程中找到的答案,但都没有成功.

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

如果可以提供对良好链接的引用,以清楚地描述不同的标签,如 v:Envelope 或 soapenv:Envelope、n0:loginservice 或 typ:loginserviceElement 或 type:loginserviceElement,我将不胜感激等

以下是android代码供参考:

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;
    }
}

来自 android 代码的响应:

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>

推荐答案

好吧,经过更多研究并得到问题的答案后,我终于让它工作了.

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

1) 虽然 SoapUi 生成 <soapenv:Envelope xmlns:soapenv="...." ....> 类型响应 xml 和使用 Ksoap2 库生成的 android 代码 <v:Envelope xmlns:i="..." ...> 类型响应 xml 具有不同的外观标签,在获取错误方面并不重要.两者很相似.

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.

如对 SO 问题的回答所述,ksoap 对 SoapEnvelope 中的命名空间具有硬编码值.

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

2) 无法识别的操作异常是由于 MAIN_REQUEST_URL 和 NAMESPACE 中的问题.知道 url、namespace 和 soap_action 的正确值有点棘手,至少对于这个领域的初学者来说是这样.

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.

这些字段的值可以通过查看请求/响应xml、wsdl和这个很好的图片示例.

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";

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

而且我也不得不改变:

String methodname = "loginservice";

String methodname = "loginserviceElement";

因为请求/响应 xml 有这个 (typ:loginserviceElement) 标签来包装属性/参数.

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

这篇关于使用KSoap2时如何在代码中创建适当的soap信封(请求xml)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆