KSOAP2 库...将数据发送到 asmx web 服务时出错

发布于 2024-12-26 10:23:59 字数 4151 浏览 1 评论 0原文

我在 Microsoft Azure 上设置了 ASMX Web 服务,我正在尝试使用 Android 应用程序将数据发送到 Web 服务并接收输出。为此,我使用 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 应用程序中,我正在发送数据,但 Web 服务仍然返回 2405 错误代码,这意味着数据未发送。

以下是我的android代码:

        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 代码后,Web 服务返回 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>

响应转储

<?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\"?>");

请帮忙!

非常感谢!

I have an ASMX webservice setup on Microsoft Azure and I'm trying to send data to the webservice and receive output using an android application. For this purpose, I am using the KSOAP library.

On the webservice, I'm checking if the strings are null. If they are, I return an error code "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
        }

In my android application, I am sending the data, but the webservice still returns the 2405 error code, which means that the data is not sent.

The following is my android code:

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

Sorry that I can provide you with the namespace, methodname, url, etc. It is against the company policy and I hope you understand. :)

Nevertheless, I'll go over the error again. After calling the above Android code, the webservice returns 2405, which according to the ASMX code is when any of the twos values are null.

UPDATE: I debugged the SOAP request (androidHttpTransport.debug = true) and got the following results for the requestDump and responseDump.

Request Dump

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

UPDATE 2
This is what the server expects:

<?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>

This is what the android application is sending:

<?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>

In addition to this, I've added the following line to the android code:

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

PLEASE HELP!

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亚希 2025-01-02 10:23:59

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

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

I was able to clear the error...The problem was that I didn't have a backslash after the namespace URI...The reason I didn't is that with the backslash I got the error "Sequence contains no elements"...Now, however, I'm having different errors...If I need help I'll post on StackOverFlow...Thanks for the generous help :)

To be clear, the namespace must have a a backslash at the end, in order for the server to receive any parameters.

甜扑 2025-01-02 10:23:59

您好
有两种方法可以解决您的问题:一种是您使用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");

尝试这个代码,它会起作用。

Hi
There are two of ways of solving your problem : one is ur way by using PropertyInfo or secondly my ways : directly adding parameter to request Parameter .

First Ways :

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

Second Ways :

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

Try this code it will work .

菩提树下叶撕阳。 2025-01-02 10:23:59

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

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

至少对我来说有帮助。我希望有人能帮忙!

加布里埃尔

at namespace it helps you write the http with capital H, like this

instead of namespace="http://tempuri.org/";
try this
namespace="Http://tempuri.org";

at least in my case has helped. I hope some body helps!

Gabriel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文