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