我有 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.
谁能帮我 ?
谢谢