我在Microsoft Azure上有一个ASMX webservice安装程序,我试图将数据发送到webservice并使用android应用程序接收输出。为此,我正在使用KSOAP库。KSOAP2 Library ...将数据发送到asmx webservice时出错
在web服务上,我检查字符串是否为空。如果是这样,我返回一个错误代码“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);
对不起,我可以为您提供的命名空间,方法名,网址等,这是对公司的政策,我希望你能理解。 :)
不过,我会再次检查错误。在调用上面的Android代码之后,web服务返回2405,根据ASMX代码返回2405,其中任何两个值都为空。
更新:我调试了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\"?>");
请帮帮我!
非常感谢!
因此,从下次..请粘贴完整的代码...:P – 2012-01-17 05:50:28
大声笑。 ..我不认为那是专业人士嗯.. :) – harsimranb 2012-01-18 06:16:29