我打電話給Web服務使用C#,我使用HttpWebRequest來調用服務,當我運行代碼時沒有錯誤,一切都按預期工作,儘管響應總是0.HttpWebRequest雖然純淨的Web服務URL工作
該Web服務應返回帳戶餘額,我發送正確的參數,這是用戶名和密碼。
調試代碼時,如果我複製服務的URL和發佈的參數並將它們粘貼到瀏覽器中,我會得到正確的響應,但來自C#的請求不起作用。
下面是代碼:
public string sInvokeService()
{
string sPostData = "userName=testITG&userPassword=MyPassword&By=nour";
string sURL = "http://mobile.net.sa/sms/gw/Credits.php";
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(sURL);
oRequest.Method = "POST";
oRequest.ContentType = "application/x-www-form-urlencoded";
oRequest.ContentLength = sPostData.Length;
string sResponse = String.Empty;
try
{
StreamWriter oStreamWriter = new StreamWriter(oRequest.GetRequestStream(), System.Text.Encoding.ASCII);
oStreamWriter.Write(sPostData);
oStreamWriter.Close();
StreamReader oStreamReader = new StreamReader(oRequest.GetResponse().GetResponseStream());
sResponse = oStreamReader.ReadToEnd();
oStreamReader.Close();
}
finally
{
}
return sResponse;
}
PS:就像我說的,如果我複製網址和參數,並在瀏覽器(http://mobile.net.sa/sms/gw/Credits.php?userName=testITG&userPassword=MyPassword&By=nour)其直接粘貼,我得到的迴應是「10 「 哪個是對的。
我想知道我是否在做任何錯誤的事情,儘管我用這個相同的代碼調用了該服務器上的另一個Web服務,它工作正常。
任何幫助將非常感激。
如果您的用戶/密碼包含需要使用UEL編碼的字符,因爲瀏覽器會爲您做這件事 – 2014-10-20 11:26:22
比較手動和基於代碼的請求與Fiddler的捕獲可能很有用 – 2014-10-20 11:26:50
如何將POST粘貼到瀏覽器? – spender 2014-10-20 11:29:19