3

我有 pdf 与 API 的描述。我必须登录他们的网络服务。Webservice 基于 REST 协议。要登录此网络服务,我必须像这样调用 url: http ://api.webepartners.pl/wydawca/Authorize?login=test&password=pass

我有账号和密码。当我用我的登录名和 psw 和过去的 url 替换 test 并通过 webbrowser 时,它看起来没问题。不会发生错误。但我必须在 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 response = request.GetResponse() as ..

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.

谁能帮我 ?

谢谢

4

2 回答 2

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>

就是这样?

于 2012-07-29T09:29:48.467 回答
1

这个 urlhttp://api.webepartners.pl/wydawca/Authorize?login=test&password=pass有一个查询字符串。由于在 a 期间不使用查询字符串值POST,也许您应该尝试 aGET吗?您使用的 HTTP 动词不正确并出现错误是有道理的405

于 2012-07-29T09:29:22.690 回答