KSOAP2 库...将数据发送到 asmx web 服务时出错
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能够清除错误...问题是我在命名空间 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.
您好
有两种方法可以解决您的问题:一种是您使用PropertyInfo的方法,第二种是我的方法:直接向请求参数添加参数。
第一种方法:
第二种方法:
尝试这个代码,它会起作用。
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 :
Second Ways :
Try this code it will work .
在命名空间,它可以帮助您用大写 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