【问题标题】:KSOAP2 Library...Error sending data to asmx webserviceKSOAP2 库...向 asmx webservice 发送数据时出错
【发布时间】:2012-01-11 20:12:53
【问题描述】:

我在 Microsoft Azure 上有一个 ASMX 网络服务设置,我正在尝试将数据发送到网络服务并使用 android 应用程序接收输出。为此,我使用了 KSOAP 库。

在网络服务上,我正在检查字符串是否为空。如果是,我返回错误代码“2405”

[WebMethod]
        public string LoginUser(string auth_token, string username)
        {
            // All these tests performed, so someone from outside of out application
            // scope does not attempt to abuse our webservice.
            #region Check for nulls
            if (string.IsNullOrEmpty(auth_token))
                return "2405";
            if (string.IsNullOrEmpty(username))
                return "2405";
            #endregion
        }

在我的android应用程序中,我正在发送数据,但是webservice仍然返回2405错误代码,这意味着数据没有发送。

以下是我的安卓代码:

        SoapObject request = new SoapObject(NAMESPACE, method_name);

        PropertyInfo pi = new PropertyInfo();
        pi.setName("auth_token");
        pi.setValue("TestAuth");
        request.addProperty(pi);

        PropertyInfo pe = new PropertyInfo();
        pe.setName("username");
        pe.setValue("TestUser");
        request.addProperty(pe);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);

对不起,我可以提供命名空间、方法名、url 等。这是违反公司政策的,希望你能理解。 :)

不过,我会再次检查错误。调用上述 Android 代码后,webservice 返回 2405,根据 ASMX 代码,这两个值中的任何一个为 null。

更新:我调试了 SOAP 请求 (androidHttpTransport.debug = true),得到了 requestDump 和 responseDump 的以下结果。

请求转储

<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>
    <LoginUser xmlns="http://tempuri.org" id="o0" c:root="1">
      <auth_token i:type="d:string">TestAuth</auth_token>
      <username i:type="d:string">TestUser</username>
    </LoginUser>
  </v:Body>
</v:Envelope>

responseDump

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <LoginUserResponse xmlns="http://tempuri.org/">
      <LoginUserResult>2405</LoginUserResult>
    </LoginUserResponse>
  </soap:Body>

</soap:Envelope>

更新 2
这是服务器所期望的:

<?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>
    <LoginUser xmlns="http://tempuri.org/">
      <auth_token />
      <username />
    </LoginUser>
  </soap:Body>
</soap:Envelope>

这是 android 应用程序发送的内容:

<?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>
        <LoginUser xmlns="http://tempuri.org" id="o0" c:root="1">
            <auth_token i:type="d:string">TestAuth</auth_token>
            <username i:type="d:string">TestUser</username>
        </LoginUser>
    </v:Body>
</v:Envelope>

除此之外,我还在 android 代码中添加了以下行:

androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

请帮忙!

非常感谢!

【问题讨论】:

    标签: android soap asmx ksoap2 android-ksoap2


    【解决方案1】:

    我能够清除错误...问题是我在命名空间 URI 之后没有反斜杠...我没有的原因是使用反斜杠时出现错误“序列不包含元素”...但是,现在,我遇到了不同的错误...如果需要帮助,我会在 StackOverFlow 上发帖...感谢您的慷慨帮助 :)

    需要明确的是,命名空间必须在末尾有一个反斜杠,以便服务器接收任何参数。

    【讨论】:

    • 哈哈...我不认为这是问题.. :)
    【解决方案2】:

    您好
    有两种方法可以解决您的问题:一种是您使用 PropertyInfo 的方法,另一种是我的方法:直接将参数添加到请求参数。

    第一种方法:

                PropertyInfo pi = new PropertyInfo();
                pi.setName("auth_token");
                pi.setValue("TestAuth");
                pi.setType(String.class);
                request.addProperty(pi);
    
                PropertyInfo pe = new PropertyInfo();
                pe.setName("username");
                pe.setValue("TestUser");
                pe.setType(String.class);
                request.addProperty(pe);
    

    第二种方法:

        SoapObject request = new SoapObject(NAMESPACE, method_name);
        request.addProperty("auth_token","TestAuth");
        request.addProperty("username","TestUser");
    

    试试这个代码,它会工作。

    【讨论】:

    • 没有区别...为了增强问题,我已经放入了 responseDump 和 requestDump 信息...请帮助!
    • 我知道它没有任何区别,但尝试过吗?
    • 我都试过了,没有任何区别...仍然遇到同样的错误...我在 UPDATE 2 部分提供了更多信息...请指教,谢谢:)
    • 你们的网络服务在公共培训上吗?来自网络服务的错误是什么请也发布..
    • 我已经为我的 IIS 服务器尝试了相同的代码 .. 它的工作原理 .. 确保您能够与 Web 服务进行通信 ..
    【解决方案3】:

    在命名空间它可以帮助你用大写 H 编写 http,像这样

    而不是命名空间="http://tempuri.org/"; 试试这个 namespace="http://tempuri.org";

    至少在我的情况下有所帮助。我希望有人能帮上忙!

    加布里埃尔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 2018-08-11
      • 2021-03-02
      • 1970-01-01
      相关资源
      最近更新 更多