[英]How to login to WebService with REST protocol
I've got pdf with a description of API. 我有一个带有API描述的pdf。 I must login into their webservice. 我必须登录他们的网络服务。 Webservice is based on REST protocol. Webservice基于REST协议。 To login into this webservice I have to call url like this: http://api.webepartners.pl/wydawca/Authorize?login=test&password=pass 要登录此网络服务,我必须像这样调用网址: http : //api.webepartners.pl/wydawca/Authorize? login = test&password = pass
I have account and password. 我有帐号和密码。 When I replace test and pass with my login and psw and past url into webbrowser it looks like is ok. 当我替换测试并通过我的登录和psw以及过去的url传递到webbrowser时,它看起来还可以。 No errors occur. 没有错误发生。 But I must do it programatically in C#. 但我必须在C#中以编程方式进行。 In google I've found: http://developer.yahoo.com/dotnet/howto-rest_cs.html 在谷歌我发现: http : //developer.yahoo.com/dotnet/howto-rest_cs.html
I try this code: 我试试这段代码:
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());
}
And I got error in the line using (HttpWebResponse response = request.GetResponse() as .. 我在使用(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.
Can anyone help me ? 谁能帮我 ?
Thanks 谢谢
You are sending a POST request with username and password as part of the request body. 您正在发送带有用户名和密码的POST请求,作为请求正文的一部分。 But it seems that the web service expects a GET service where everything is in the URL of the request. 但似乎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>
That's it? 而已?
This url http://api.webepartners.pl/wydawca/Authorize?login=test&password=pass
has a query string. 此网址http://api.webepartners.pl/wydawca/Authorize?login=test&password=pass
有一个查询字符串。 Since query string values are not used during a POST
, maybe you should try a GET
instead? 由于在POST
期间不使用查询字符串值,您可能应该尝试使用GET
吗? It would make sense that the HTTP verb you are using is incorrect with a 405
error. 你使用的HTTP动词是错误的405
错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.