Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# REST API Client [closed]

Tags:

rest

c#

wpf

api

I have successfully created a PHP REST API which resides on my server. I am now looking to create the client-side connection to this via my WPF C# application. I found this but my API requires the API key to be sent via a HTTP header, and I can't see you can do that in this. I also created a PHP REST client using CURL and it was very easy, and was hoping that there would be something built into C# to handle requests to REST services.

If someone could point me in the direction of a tutorial they have seen, or a library somewhere I would be grateful.

Thanks.

like image 407
Joel Kennedy Avatar asked Feb 11 '11 15:02

Joel Kennedy


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

Take a look at RESTSharp. Very powerful, and easy to use.

Works on all platforms too: Web, Windows, WCF, Monotouch, Windows Phone

like image 136
Chris Kooken Avatar answered Oct 11 '22 01:10

Chris Kooken


You can just use HttpWebRequest or WebClient to make web requests like you would have with CURL in your PHP client...

If you need to deal with JSON based responses, JSON.Net is a fantastic library.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://a/rest/uri"); request.Method = "POST"; request.Headers.Add("Authorization: OAuth " + accessToken); string postData = string.Format("param1=something&param2=something_else"); byte[] data = Encoding.UTF8.GetBytes(postData);  request.ContentType = "application/x-www-form-urlencoded"; request.Accept = "application/json"; request.ContentLength = data.Length;  using (Stream requestStream = request.GetRequestStream()) {     requestStream.Write(data, 0, data.Length); }  try {     using(WebResponse response = request.GetResponse())     {         // Do something with response     } } catch (WebException ex) {     // Handle error } 
like image 28
Neil M Avatar answered Oct 11 '22 00:10

Neil M