views:

1077

answers:

5

Hi All,

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?

All your help will be appreciated.

Ajish.

+1  A: 

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
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 sayingThis 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
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
How can I change/set the date header in .NET httpRequest? When I try to change it, I am getting an error.
Ajish
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
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
A: 

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
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
+2  A: 

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
even here I am not able to set/change the date header.
Ajish
Why not? Do you get an error?
Darrel Miller
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
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
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
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
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
+1  A: 

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());
mattRo55
A: 

here is a good post try to explain how to consume rest based services i hope help u

http://blog.flair-systems.com/2010/05/how-to-consume-rest-based-services.html

Waleed Mohamed
Apart from my eyes straining to read that snippet (could use some whitespace) does anybody actually use a streamwriter directly for this?
Joshua Hayes