将 HttpClient 与 RightScale API 结合使用
我正在尝试将 WCF Rest 入门套件与 RightScale 的登录 API 使用起来似乎相当简单。
编辑 - 这是我写的关于使用 Powershell 的博客条目使用 API。
编辑 - 为 RightScale API 创建了一个通用的 .NET 包装器 - NRightAPI
它完全一样简单使用 CURL 时看起来如此。为了让我获得登录 cookie,我需要做的就是:
curl -v -c rightcookie -u 用户名:密码“https ://my.rightscale.com/api/acct/accountid/login?api_version=1.0"
我收到以下 cookie:
HTTP/1.1 204 无内容 日期:25 日星期五
2009 年 12 月 12:29:24 GMT 服务器:Mongrel 1.1.3 状态:204 无内容 X 运行时:0.06121
内容类型: 文本/html;字符集=utf-8
内容长度:0
缓存控制: 无缓存
添加了 cookie _session_id="488a8d9493579b9473fbcfb94b3a7b8e5e3" 对于域 my.rightscale.com,路径 /, 过期 0
设置 Cookie: _session_id=488a8d9493579b9473fbcfb94b3a7b8e5e3; 路径=/;安全变化:接受编码
但是,当我使用以下 C# 代码时:
HttpClient http = 新 HttpClient("https://my.rightscale.com/api/accountid/login ?api_version=1.0");
http.TransportSettings.UseDefaultCredentials = 假;
http.TransportSettings.MaximumAutomaticRedirections = 0;
http.TransportSettings.Credentials = 新的 NetworkCredential("用户名", “密码”);
Console.WriteLine(http.Get().Content.ReadAsString());
我得到的不是 HTTP 204,而是重定向:
您正在 href="https://my.rightscale.com/dashboard">重定向
如何让 WCF REST 入门套件与 RighScale API 配合使用?
I'm trying to use the WCF Rest Starter Kit with the RightScale's Login API which seems fairly simple to use.
Edit - Here's a blog entry I wrote on using Powershell to consume the API.
Edit - Created a generic .NET wrapper for the RightScale API - NRightAPI
It's exactly as simple as it looks while using CURL. In order for me to obtain a login cookie all I need to do is:
curl -v -c rightcookie -u username:password "https://my.rightscale.com/api/acct/accountid/login?api_version=1.0"
And I receive the following cookie:
HTTP/1.1 204 No Content Date: Fri, 25
Dec 2009 12:29:24 GMT Server: Mongrel
1.1.3 Status: 204 No Content X-Runtime: 0.06121
Content-Type:
text/html; charset=utf-8
Content-Length: 0
Cache-Control:
no-cache
Added cookie
_session_id="488a8d9493579b9473fbcfb94b3a7b8e5e3" for domain my.rightscale.com, path /,
expire 0
Set-Cookie:
_session_id=488a8d9493579b9473fbcfb94b3a7b8e5e3;
path=/; secure Vary: Accept-Encoding
However, when I use the following C# code:
HttpClient http = new
HttpClient("https://my.rightscale.com/api/accountid/login?api_version=1.0");
http.TransportSettings.UseDefaultCredentials
= false;
http.TransportSettings.MaximumAutomaticRedirections
= 0;
http.TransportSettings.Credentials =
new NetworkCredential("username",
"password");
Console.WriteLine(http.Get().Content.ReadAsString());
Instead of a HTTP 204, I get a redirect:
You are being <a>
href="https://my.rightscale.com/dashboard">redirected <a>
How do I get the WCF REST starter kit working with the RighScale API ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要在我的请求中添加一个“Authorization: Basic”标头。
除了我发布的初始代码之外:
我需要添加授权标头以及带有用户名/密码的 REST 请求,如下所示:
然后当我提出请求时:
我收到了 HTTP 204 以及我正在寻找的会话 cookie。我能说什么,Fiddler 太棒了:)!
I needed to add a "Authorization: Basic " header to my request.
In additional to the initial code I had posted:
I need to add the Authorization header along with the REST request with the username/password as follows:
And then when I made the request:
I received the HTTP 204 along with the session cookie I was looking for. What can I say, Fiddler is awesome :) !