C# 如何使用REST协议登录WebService

C# 如何使用REST协议登录WebService,c#,web-services,rest,httpwebrequest,C#,Web Services,Rest,Httpwebrequest,我有一个描述API的pdf。我必须登录他们的网络服务。Webservice基于REST协议。要登录此Web服务,我必须按如下方式调用url: 我有帐户和密码。当我用我的登录名、psw和过去的url将测试和通过替换为webbrowser时,看起来一切正常。没有错误发生。 但我必须用C语言编程。 在谷歌,我发现: 我尝试以下代码: Uri address = new Uri(@"http://api.webepartners.pl/wydawca/Authorize");

我有一个描述API的pdf。我必须登录他们的网络服务。Webservice基于REST协议。要登录此Web服务,我必须按如下方式调用url:

我有帐户和密码。当我用我的登录名、psw和过去的url将测试和通过替换为webbrowser时,看起来一切正常。没有错误发生。 但我必须用C语言编程。 在谷歌,我发现:

我尝试以下代码:

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()as..的行中得到了错误

有人能帮我吗


谢谢

这个url
http://api.webepartners.pl/wydawca/Authorize?login=test&password=pass
有一个查询字符串。由于在
POST
过程中不使用查询字符串值,也许您应该尝试使用
GET
?您正在使用的HTTP谓词不正确且出现
405
错误是有道理的。

您正在发送一个带有用户名和密码的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());
}
。。。。。

GEThttp://api.webepartners.pl/wydawca/Authorize?from=2012-07-29%2013:47:05 HTTP/1.1
内容类型:text/xml;编码='utf-8'
主机:api.webepartners.pl
Cookie:.ASPXAUTH=7521F26EBCE8CE487C6860C5E98248B540E5591B1BD6AE7EC936ECE29B0912AC49C71837B98D7972ABA9C868F18A0C6FCD1EB38B22BE86DBCCCDF8D56D0440170FECF497FF00A1B5D7B2268EF6B2B9DB806291E517654A136EC5617A67182DB3ECF0D8ADA6927C2F955A920B7BF7AE6D7AED7AED4AED9AED4EA920E20E20E20CFD7B7B7B7D7CFD4
HTTP/1.1 403禁止
缓存控制:没有缓存
Pragma:没有缓存
内容类型:text/html
过期:-1
服务器:Microsoft IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
日期:2012年7月29日星期日格林尼治标准时间11:47:03
内容长度:1233
403-禁止:访问被拒绝。
服务器错误
403-禁止:访问被拒绝。
您没有使用您提供的凭据查看此目录或页面的权限。

就这样?

好的,谢谢,它能工作。但现在我必须获得下一个信息:。所以我必须创建第二个请求?授权是什么?我没有更多文档。我创建CookieContainer cookies=new CookieContainer();并将其分配给myHttpWebRequest.CookieContainer=cookies;我可以看到我添加了cookie,然后我使用新url创建myHttpWebRequest1,并分配myHttpWebRequest1.CookieContainer=cookies;但是当我调用HttpWebResponse时,myHttpWebResponse1=(HttpWebResponse)myHttpWebRequest1.GetResponse();我得到错误:…禁止。因此身份验证是错误的。根据给定的信息,很难判断问题出在哪里。您可以使用网络监视器跟踪客户端和服务器之间的通信并发布传输的请求和响应吗?get/wydawca/Authorize?login=testlog&password=F8-06-0F-23-67-26-FC-E1-45-99-D6-75-45-1D-B7-93 HTTP/1.1 HTTP/1.1 200正常缓存控制:无缓存Pragma:无缓存过期:-1服务器:Microsoft IIS/7.5 X-AspNet-Version:4.0.30319设置Cookie:.ASPXAUTH=0cc3337fcbb70847419bb71f3017edc24f6f746bf64acc8dd4dfdbb9f7d1035afd411cdfcc8040693caf5b685c45ccd697d9a836bea6725c565ad365b532525258b758b758b752b758b758b758b758b758b758b752b758b758b758b758b758b758f7b758b758b75C86AE794BE3BD21510365A470DD7A29914E990506FD56845CA5CB5C08445342240DE223FE22D7AFA5;路径=/;HttpOnly X-Powered-By:ASP.NET日期:Sun,2012年7月29日10:38:00 GMT内容长度:0请将其附加到您的问题中,并显示第二个请求/响应。
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>