将 HttpClient 与 RightScale API 结合使用

发布于 2024-08-16 18:51:57 字数 1948 浏览 8 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白首有我共你 2024-08-23 18:51:57

我需要在我的请求中添加一个“Authorization: Basic”标头。

除了我发布的初始代码之外:

HttpClient http = new HttpClient("https://my. rightscale.com/api/acct/accountid/login?api_version=1.0");
http.TransportSettings.UseDefaultCredentials = false;
http.TransportSettings.MaximumAutomaticRedirections = 0;
http.TransportSettings.Credentials = new NetworkCredential("用户名", "密码");

我需要添加授权标头以及带有用户名/密码的 REST 请求,如下所示:

字节[] authbytes =
Encoding.ASCII.GetBytes(string.Concat("用户名",":", "密码"));
字符串base64 = Convert.ToBase64String(authbytes);
stringauthorization = string.Concat("授权:基本", base64);
http.DefaultHeaders.Add(授权);

然后当我提出请求时:

Console.WriteLine(http.Get().Content.ReadAsString());

我收到了 HTTP 204 以及我正在寻找的会话 cookie。我能说什么,Fiddler 太棒了:)!

I needed to add a "Authorization: Basic " header to my request.

In additional to the initial code I had posted:

HttpClient http = new HttpClient("https://my.rightscale.com/api/acct/accountid/login?api_version=1.0");
http.TransportSettings.UseDefaultCredentials = false;
http.TransportSettings.MaximumAutomaticRedirections = 0;
http.TransportSettings.Credentials = new NetworkCredential("username", "password");

I need to add the Authorization header along with the REST request with the username/password as follows:

byte[] authbytes =
Encoding.ASCII.GetBytes(string.Concat("username",":", "password"));
string base64 = Convert.ToBase64String(authbytes);
string authorization = string.Concat("Authorization: Basic ", base64);
http.DefaultHeaders.Add(authorization);

And then when I made the request:

Console.WriteLine(http.Get().Content.ReadAsString());

I received the HTTP 204 along with the session cookie I was looking for. What can I say, Fiddler is awesome :) !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文