0

I'm trying to use HttpClient to read the response content from a 3rd party API (Rackspace Cloud Files). Here's what I have so far. I can't seem to get the content.

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("X-Auth_User", username);
    client.DefaultRequestHeaders.Add("X-Auth-Key", api);
    client.GetAsync("identity.api.rackspacecloud.com".ToAbsoluteUrl()).ContinueWith(
        (requestTask) =>
        {
            HttpResponseMessage response = requestTask.Result;
            response.EnsureSuccessStatusCode();
            response.Content.ReadAsAsync<string>().ContinueWith(
                      (readTask) =>
                      {
                          var result = readTask.Result;
                      });
        });

This gives me "No 'MediaTypeFormatter' is available to read an object of type 'String' with the media type 'text/html'." error.

I need to retrieve the response details as noted in the Rackspace docs (example):

HTTP/1.1 204 No Content
Date: Mon, 12 Nov 2007 15:32:21 GMT
X-Storage-Url: https://storage.clouddrive.com/v1/CF_xer7_34
X-CDN-Management-Url: https://cdn.clouddrive.com/v1/CF_xer7_34
X-Auth-Token: eaaafd18-0fed-4b3a-81b4-663c99ec1cbb
Content-Length: 0
Content-Type: text/plain; charset=UTF-8

How do I get the response?

When I use ReadAsStringAsync, it gives my the HTML source of my page.

Thank you.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Rivka
  • 2,172
  • 10
  • 45
  • 74
  • If it's sending you `text/html`, you're probably sending it the wrong request. What are you trying to read? – SLaks Nov 06 '12 at 19:00
  • I'm trying to get the auth token (see url above "Rackspace Cloud Files") – Rivka Nov 06 '12 at 19:01
  • I think it can solve: http://stackoverflow.com/questions/8367871/wcf-web-api-preview-6-no-mediatypeformatter-is-available – Heitor Nov 06 '12 at 19:04
  • So you're trying to read the header, not the content? – SLaks Nov 06 '12 at 19:06
  • Oh.. yeah guess so. So you're right - wrong request because the auth token header isn't being returned. – Rivka Nov 06 '12 at 19:09
  • @SLaks you made me realize - the request address was wrong. Let me try tweaking.. – Rivka Nov 06 '12 at 19:12
  • In the future, when you have a problem like this, think carefully about what you're trying to accomplish and how each line of code that you wrote does that. You can frequently solve this kind of problem by yourself just by careful thinking – SLaks Nov 06 '12 at 19:29
  • @SLaks while I agree with you on a fundamental level, there is a reason why peer review is useful. Sometimes when a problem is staring you in the face, you miss it and someone else see's it or pushes you look to more closely. We are human and SO is filled with helpful people, right? helpful?:D – Hardrada Nov 06 '12 at 20:12
  • I'm still stuck on this - I can't figure out what the request is meant to be. This doc: http://docs.rackspace.com/auth/api/v1.1/auth-client-devguide-20120731.pdf seems to indicate a POST is required, while the first doc that I referenced in my OP indicates a GET... besides I can't figure out the PostAsync Syntax. Not expecting you guys to do the work for me, but if someone has exp. with this API, I'd appreciate the guidance. – Rivka Nov 06 '12 at 20:51
  • I ended up using HttpWebRequest for this. – Rivka Nov 26 '12 at 16:50

0 Answers0