KSOAP2 Library…Error sending data to asmx webservice

陌路散爱 提交于 2019-12-10 19:06:45

问题


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!


回答1:


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.




回答2:


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 .




回答3:


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



来源:https://stackoverflow.com/questions/8825951/ksoap2-library-error-sending-data-to-asmx-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!