我已經得到了有API描述的pdf。我必須登錄到他們的web服務。 Webservice基於REST協議。要登錄到這個網絡服務,我必須打電話這樣的網址: http://api.webepartners.pl/wydawca/Authorize?login=test&password=pass如何使用REST協議登錄到WebService
我有帳戶和密碼。當我更換測試,並通過我的登錄名和psw以及過去的網址進入網頁瀏覽器時,它看起來沒問題。沒有錯誤發生。 但我必須在C#中以編程方式執行此操作。 在谷歌,我發現: http://developer.yahoo.com/dotnet/howto-rest_cs.html
我試試這個代碼:
Uri address = new Uri(@"http://api.webepartners.pl/wydawca/Authorize");
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Set type to POST
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// Create the data we want to send
//string appId = "YahooDemo";
//string context = "Italian sculptors and painters of the renaissance"
// + "favored the Virgin Mary for inspiration";
//string query = "madonna";
string userName = "mylogin";
string passsword = "mypassword";
StringBuilder data = new StringBuilder();
//data.Append("appid=" + HttpUtility.UrlEncode(appId));
//data.Append("&context=" + HttpUtility.UrlEncode(context));
//data.Append("&query=" + HttpUtility.UrlEncode(query));
data.Append("login=" + HttpUtility.UrlEncode(userName));
data.Append("&password=" + HttpUtility.UrlEncode(passsword));
// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
// Set the content length in the request headers
request.ContentLength = byteData.Length;
// Write data
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) // error
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
}
我使用(HttpWebResponse響應= request.GetResponse()如遇到錯誤在該行..
An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
Additional information: The remote server returned an error: (405) Method Not Allowed.
誰能幫我?
感謝
好的感謝它的工作。但現在我必須得到下一個信息:http://api.webepartners.pl/wydawca/Auctions?from = date。所以我必須創建第二個請求?什麼授權? – Robert 2012-07-29 09:47:20
我沒有更多文檔。我創建CookieContainer cookie = new CookieContainer();並將其分配給myHttpWebRequest.CookieContainer = cookies;我可以看到我添加了cookie,然後用新的url創建了myHttpWebRequest1,並且指定了myHttpWebRequest1.CookieContainer = cookies;但是當我調用HttpWebResponse時myHttpWebResponse1 =(HttpWebResponse)myHttpWebRequest1.GetResponse();我得到錯誤:...禁止。所以認證是錯誤的。 – Robert 2012-07-29 10:15:51
根據給定的信息,很難確定問題出在哪裏。您可以使用網絡監視器來跟蹤客戶端和服務器之間的通信併發送傳輸的請求和響應嗎? – Codo 2012-07-29 10:21:43