4

just starting to create an API to my a web application using the ASP.NET MVC4 Web API project template. http://www.asp.net/mvc/mvc4

No problems with the API so far, but I was about to write a small C# app to test the API.

Almost all the sample I can find is using the a class called HttpClient.

Where can I find the HttpClient and how do I install it?

4 Answers 4

9

Rather than using the build in HttpClient class of the .NET framework which has a lot of issues when dealing with StatusCodes that are different than the expected ones. I recommend using a library called RestSharp.

It has become .NET Http/Rest client of choice, you can get it here: http://restsharp.org/

It is a very powerful library that is perfectly suited to do what you want.

Sign up to request clarification or add additional context in comments.

4 Comments

OK, thanks for the suggestion, will check it out. What I like with the HttpClient is the easy Get, Post, Delete syntax.
RestSharp is build for accessing WebAPIs, so rest assured handling Get, Put, Delete, Post etc. is extremely easy. Also and that's a huge benefit for me is that it takes care of serialization/deserialization. This really cuts down the development effort to a minimum: github.com/restsharp/RestSharp/wiki/Deserialization
Out of curiosity, could you link to anywhere showing the statuscodes problem with httpclient in .net 4.5?
The is a totally new HttpClient in .NET 4.5 (which is still beta though), it's much better for dealing with WebApi's by now, but RestSharp has proven to be a very effective library for writing WebApi clients, so that's my recommendation for now
8

It's on nuget, search for HttpClient

http://nuget.org/packages/System.Net.Http

Comments

3

Use WebRequest as described here

        // Create a new 'Uri' object with the specified string.
        Uri myUri =new Uri("http://www.contoso.com");
        // Create a new request to the above mentioned URL. 
        WebRequest myWebRequest= WebRequest.Create(myUri);
        // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
        WebResponse myWebResponse= myWebRequest.GetResponse();

If its a REST interface use RestSharp but you would need XSD first.

Comments

1

If the class is not available from your code, then you could download it from a NuGet package, like described in the article:

http://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee

or you can try to locate it inside the namespace: System.Net.Http

There is also an example for you wich should get you started!

Comments

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.