23

I am facing some problems when using the HttpClient class to access to a Delicious API. I have the following code:

try
{
    const string uriSources = "https://api.del.icio.us/v1/tags/bundles/all?private={myKey}";
    using (var handler = new HttpClientHandler { Credentials = new  
                             NetworkCredential("MyUSER", "MyPASS") })
    {
         using (var client = new HttpClient(handler))
         {
             var result = await client.GetStringAsync(uriSources);
         }
   }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "ERROR...", MessageBoxButton.OK);
}

When running the code above I am getting the following: Response status code does not indicate success: 401 (Unauthorized).

So, how could I get this work? Is it possible?

Thanks in advance

Regards!

Adam Szabo
  • 11,302
  • 18
  • 64
  • 100
MikePR
  • 2,786
  • 5
  • 31
  • 64
  • 401 Unauthorized means Incorrect User Name or Password... – Raptor Aug 28 '13 at 02:37
  • I know, however I am using the correct ones ;-) – MikePR Aug 28 '13 at 02:40
  • When I put the above url in any browser it ask me for the credencial (the same I am using in the code) and it works. So, I guess it is related the way how the httpClient is requesting the data – MikePR Aug 28 '13 at 02:42
  • This may seem a silly question, but you are actually using your private key instead of {myKey} and youve just put that in there for the purpose of demonstration right? – Melvin DVaz Aug 28 '13 at 02:45
  • That's right. I put it just for demostration purpose as well as the user name and password. I am using the good ones in my sandbox ;-) – MikePR Aug 28 '13 at 02:47
  • Is it possible that you need to specify a domain for your network credentials? Thats the only thing I can think of for an authorize error when your login details are correct :( – Melvin DVaz Aug 28 '13 at 02:54
  • Maybe strange to reply that but... sometimes I've that kind of behavior and in fact I lost the Internet connection in my WP emulator. Try to launch IE. – Fabrice Aug 28 '13 at 06:20
  • 1
    Have you tried using HttpWebRequest, http://blog.karlbunyan.com/2007/02/11/asp-net-del-icio-us-api-integration-with-c/ – Erwin Aug 28 '13 at 07:50
  • Hi Erwin. I haven't had the time to test your suggestion until now. and it works OK for me. The only concern is that the HttpWebRequest is executed in the same thread of the UI. But, I only need it to retrieve a few data. So, I guess this will be Ok. Thanks ;-) – MikePR Aug 30 '13 at 03:22

3 Answers3

35

I had the exact same problem myself. It seems the HttpClient just disregards the credentials set in the HttpClientHandler.

The following shall work however:

using System.Net.Http.Headers; // For AuthenticationHeaderValue

const string uri = "https://example.com/path?params=1";
using (var client = new HttpClient()) {
    var byteArray = Encoding.ASCII.GetBytes("MyUSER:MyPASS");
    var header = new AuthenticationHeaderValue(
               "Basic", Convert.ToBase64String(byteArray));
    client.DefaultRequestHeaders.Authorization = header;

    var result = await client.GetStringAsync(uri);
}

No need for the handler.

Source: http://www.snip2code.com/Snippet/13895/Simple-C---NET-4-5-HTTPClient-Request-Us

sharptooth
  • 167,383
  • 100
  • 513
  • 979
Adam Szabo
  • 11,302
  • 18
  • 64
  • 100
  • 3
    This solution works well as it allows for the Authorization header to be set on the first request, thus bypassing the 401 Challenge / Response. In my experience with HttpClient and the HttpClientHandler, however, setting pre-authenticate does work in that the Authorize header is set on every request after the 401 Challenge / Response is completed. This article here explains it well: http://weblog.west-wind.com/posts/2010/Feb/18/NET-WebRequestPreAuthenticate-not-quite-what-it-sounds-like – Philippe Sep 24 '14 at 18:56
  • 2
    @Adam Szabo [this](https://gist.github.com/bryanbarnard/8102915) seems the original source – superjos Aug 05 '15 at 13:51
2

This is an old post but thought to add my reply for someone facing similar issue and browsing answers...

I faced similar issue. In my case, setting Domain property for NetworkCredentials worked. You can try setting Domain.

Nirav Darji
  • 128
  • 1
  • 6
0

The code you are showing works for me against an authenticated resource. I suspect Delicious is doing something weird.

Considering you are on Windows Phone, it is a pain to debug with Fiddler, so what I suggest is getting a Runscope account. Install this message handler which will redirect your request via the RunScope debugger. Once you do this, I suggest you look at the www-authenticate header and examine what that is returning.

If all else fails you can always set the Authentication header directly with basic auth credentials. You don't need to use the Credentials class.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • I have a similar issue posted [here](https://stackoverflow.com/q/64289957/1232087) in case you get a chance for a suggestion/your thoughts etc. – nam Oct 10 '20 at 23:23