Using HttpClient with RightScale API

I am trying to use the WCF Rest Starter Kit with the RightScale Login API , which seems pretty easy to use.

Edit - here's the entry blog I wrote about using Powershell to use the API.

Edit - Created a common .NET wrapper for the RightScale API - NRightAPI

It is just as easy as using CURL. In order to get the login cookie, I need to do the following:

curl -v -c rightcookie -u username: password " https://my.rightscale.com/api/acct/accountid/login?api_version=1.0 "

And I get the following cookie:

HTTP / 1.1 204 No data Date: Fri, 25
December 2009 12:29:24 GMT Server: Mongrel 1.1.3 Status: 204 No X-Runtime content: 0.06121
Content-Type: text / html; charset = utf-8
Content-Length: 0
Cache-control: no-cache
Added cookie _session_id = "488a8d9493579b9473fbcfb94b3a7b8e5e3" for the 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");
(http.Get () Content.ReadAsString ().);

Instead of HTTP 204, I get a redirect:

You are <a> href = "https://my.rightscale.com/dashboard"> redirected <a>

How do I get a WCF REST starter kit that works with the RighScale API?

+4
source share
1 answer

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

In addition to the source code I 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 an authorization header along with a REST request with 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:

eNP (http.Get () Content.ReadAsString ().);

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

+6
source

All Articles