2012-07-29 64 views
3

我已經得到了有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. 

誰能幫我?

感謝

回答

3

您正在發送帶有用戶名和密碼的POST請求作爲請求主體的一部分。但似乎Web服務需要一個GET服務,其中的一切都在請求的URL中。

String uriStr = @"http://api.webepartners.pl/wydawca/Authorize?login=" 
    + HttpUtility.UrlEncode(userName) + "&password=" + HttpUtility.UrlEncode(passsword); 
Uri address = new Uri(uriStr); 
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; 

// Get response 
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) // error 
{ 
    // Get the response stream 
    StreamReader reader = new StreamReader(response.GetResponseStream()); 
} 

..... .....

GET http://api.webepartners.pl/wydawca/Authorize?from=2012-07-29%2013:47:05 HTTP/1.1 
Content-Type: text/xml; encoding='utf-8' 
Host: api.webepartners.pl 
Cookie: .ASPXAUTH=7521F26EBCE8CE487C6860C5E98248B540E5591BD6AE7EC936ECE29B0912AC49C71837B98D7972ABA9C868F18A0C6FCD1EB38B22BE86DBCCCDF8D56D0440170FECF497FF00A1B5D7B268EF6DF27B2B9DB806291E517654A136EC5617A67182DB3E3ECF0D8ADA6F3927C2F955A92E20B7BF7AE6D7DAE2AED0B0D9A7BD406C2CF4 


HTTP/1.1 403 Forbidden 
Cache-Control: no-cache 
Pragma: no-cache 
Content-Type: text/html 
Expires: -1 
Server: Microsoft-IIS/7.5 
X-AspNet-Version: 4.0.30319 
X-Powered-By: ASP.NET 
Date: Sun, 29 Jul 2012 11:47:03 GMT 
Content-Length: 1233 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> 
<title>403 - Forbidden: Access is denied.</title> 
<style type="text/css"> 
<!-- 
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;} 
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;} 
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF; 
background-color:#555555;} 
#content{margin:0 0 0 2%;position:relative;} 
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;} 
--> 
</style> 
</head> 
<body> 
<div id="header"><h1>Server Error</h1></div> 
<div id="content"> 
<div class="content-container"><fieldset> 
    <h2>403 - Forbidden: Access is denied.</h2> 
    <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3> 
</fieldset></div> 
</div> 
</body> 
</html> 

就這樣?

+0

好的感謝它的工作。但現在我必須得到下一個信息:http://api.webepartners.pl/wydawca/Auctions?from = date。所以我必須創建第二個請求?什麼授權? – Robert 2012-07-29 09:47:20

+0

我沒有更多文檔。我創建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

+0

根據給定的信息,很難確定問題出在哪裏。您可以使用網絡監視器來跟蹤客戶端和服務器之間的通信併發送傳輸的請求和響應嗎? – Codo 2012-07-29 10:21:43

1

此網址http://api.webepartners.pl/wydawca/Authorize?login=test&password=pass有一個查詢字符串。由於在POST期間未使用查詢字符串值,因此您應該嘗試使用GET來代替?這是有道理的,你使用的HTTP動詞是不正確的,並出現405錯誤。