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 via 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?

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

9 Comments

even here I am not able to set/change the date header.
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.'.
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.
Hmmm, good news and bad news. Apparently it is supported in .Net 4 msdn.microsoft.com/en-us/library/…
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?
|
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());

Comments

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.

5 Comments

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
Looks like this isn't going to work. social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/… I would look at Darrel's comment.
How can I change/set the date header in .NET httpRequest? When I try to change it, I am getting an error.
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.
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?
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.