9

I am trying to consume REST API from my .NET Application. This API's are all written in JAVA. I am asked to pass the authentication credentials vis HTTP headers. How can I pass these authentication credentials like 'DATE', 'AUTHORIZATION' and 'Accept' via HTTP headers.

Which class in .NET can I use to accomplish this task. Can anyone help me with this?

Ajish
  • 135
  • 1
  • 2
  • 6

4 Answers4

13

Update

This library has now been replaced by http://nuget.org/packages/Microsoft.Net.Http/2.1.10


Use the Microsoft.Http client library that is in WCF REST Starter Kit Preview 2.

Here is how you could use it:

    var client = new HttpClient();
    client.DefaultHeaders.Authorization = new Credential("ArbitraryAuthHeader");
    client.DefaultHeaders.Date = DateTime.Now;
    client.DefaultHeaders.Accept.Add("application/xml");

    var response = client.Get("http://example.org");

    var xmlString = response.Content.ReadAsString();
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • even here I am not able to set/change the date header. – Ajish Dec 07 '09 at 22:37
  • When I tried to set date header, I am getting an exception saying 'An exception occurred during a WebClient request.'. And I tried without a date header, I am getting a 401 unauthorized error like this 'The remote server returned an error: (401) Unauthorized.'. – Ajish Dec 08 '09 at 18:55
  • My apologies. I dug into the source code for HttpClient and you are correct that setting the Date header is not supported. According to a comment in the code, the author does not know how to do it. I wrongly assumed that because there was a set accessor on the method that it was supported. Apparently the library authors intended to support it. – Darrel Miller Dec 08 '09 at 22:56
  • Hmmm, good news and bad news. Apparently it is supported in .Net 4 http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.date(VS.100).aspx – Darrel Miller Dec 08 '09 at 23:00
  • Darrel, Is it possible to use System.NET.Sockets.TcpClient to send and receive request to a remote server? And most importantly, is it possible to pass the httpheaders through this Class? – Ajish Dec 09 '09 at 16:43
  • One would assume it is possible. At the core it is just text going over a socket. However, it sounds like an awful lot of work to get a client to tell the server what the date is. Are you absolutely sure the Java API requires that the client specify the Date header? – Darrel Miller Dec 09 '09 at 17:14
  • As of July 2013, it seems that it is not supported anymore. Have a look at RestSharp here : https://github.com/restsharp/RestSharp – MaxiWheat Jul 03 '13 at 19:40
  • @MaxiWheat The official released version is here http://nuget.org/packages/Microsoft.Net.Http/2.1.10 – Darrel Miller Jul 03 '13 at 19:43
  • Great, thank you Darrel for the update, I did not find the new version when I was searching for – MaxiWheat Jul 04 '13 at 01:18
4

Just to add a bit of value to this thread (I too was looking for a way to consume a RESTful service and easily provide credentials and came across this thread ... I did not have the "Date" requirement), Aaron Skonnard has written an excellent article on using the WCF REST Starter Kit called:

A Developer's Guide to the WCF REST Starter Kit

There is a very informative section on how to consume a RESTful service using HttpClient. And here's the code snippet to talk to Twitter:

HttpClient http = new HttpClient("http://twitter.com/statuses/");
http.TransportSettings.Credentials =
    new NetworkCredential("{username}", "{password}");
HttpResponseMessage resp = http.Get("friends_timeline.xml");
resp.EnsureStatusIsSuccessful();
ProcessStatuses(resp.Content.ReadAsStream());
autonomatt
  • 4,393
  • 4
  • 27
  • 36
1

There are a number of ways taht you can do this but using the WebRequest objects is the fastest if you have just a few calls to complete.

This site, has a great overview of the process.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • I tried using the same HttpWebRequest and HttpWebResponse class to send request and get response respectively. But right now the problem is I am not able to add/set the DATE header of HttpWebRequest. When I try to set the DATE header. I am getting an exception saying This header must be modified using the appropriate property. Parameter name: name. I tried to google a solution for this but no go.. Is there anything I can do to set the DATE header in HttpWebRequest – Ajish Dec 03 '09 at 21:11
  • Looks like this isn't going to work. http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/dba65027-3fb8-42ec-83e8-90bd77c42ca3/ I would look at Darrel's comment. – Mitchel Sellers Dec 07 '09 at 19:41
  • How can I change/set the date header in .NET httpRequest? When I try to change it, I am getting an error. – Ajish Dec 07 '09 at 21:29
  • Following the link I have listed, this is NOT possible, it isn't supported by the .NET framework. You would have to roll your own process. – Mitchel Sellers Dec 07 '09 at 23:06
  • how can I create my own process for simulating this Scenario. I tried with TCPClient in System.Net.Sockets class. But i am not sure how to pass the headers in TCpClient. Is there any other to achieve this? – Ajish Dec 08 '09 at 18:52
0

Despite its somewhat misleading name, ADO.NET Data Services (which is part of .NET 3.5) contains APIs for both exposing and consuming REST-based services. In your case you can safely ignore the part that allows you to expose services and concentrate on the client part.

It supports LINQ and all sorts of goodness, allowing you to query your REST service like this:

var selectedOrders = from o in context.Orders
                     where o.Freight > 30
                     orderby o.ShippedDate descending 
                     select o;

There's more about it here. Give it a try - I've been pretty happy with it so far.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 3
    The problem with the example that you are giving is that you can only do the linq query against ADO.Net Data Service endpoints. You cannot do that against other REST endpoints. The OP will not be able to do this against his Java APIs. – Darrel Miller Dec 04 '09 at 14:56