Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making and receiving an HTTP request in C#

I want to make my C# application to be able to send an http request and receive the answer at runtime

an explanation from the website I want to request from is HERE

I don't have any experience with that before, so I'm a little confused about the JSON, XML stuff I know I'll need an XML parser or something like this to understand the request

like image 236
smohamed Avatar asked Oct 27 '11 14:10

smohamed


People also ask

What is HTTP request and write syntax?

HTTP Requests are messages which are sent by the client or user to initiate an action on the server. The first line of the message includes the request message from the client to the server, the method which is applied to the resource, identifier of the resource, and the protocol version. Syntax. Request = Request-Line.

What is HTTP request and HTTP response with example?

HTTP works as a request-response protocol between a client and server. Example: A client (browser) sends an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content.

What is the process for a HTTP request?

An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server. To make the request, the client uses components of a URL (Uniform Resource Locator), which includes the information needed to access the resource.


4 Answers

Making a HTTP request is very simple if you don't want to customize it: one method call to WebClient.DownloadString. For example:

var client = new WebClient();
string html = client.DownloadString("http://www.google.com");
Console.WriteLine(html);

You will need to build the correct URL each time as per the documentation you link to.

If you use the example code above to talk to your API, html (which is really the response data in general) will contain either XML or JSON as a string. You would then need to parse this into some other type of object tree so that you can work with the response.

like image 87
Jon Avatar answered Sep 20 '22 16:09

Jon


Apart from using WebClient as suggested, you could also have a look at EasyHttp by Hadi Hariri from JetBrains. You can find it at https://github.com/hhariri/EasyHttp Summary from ReadMe:

EasyHttp - An easy to use HTTP client that supports:

  • HEAD, PUT, DELETE, GET, POST
  • Cookies
  • Authentication
  • Dynamic and Static Typing
  • XML, JSON and WWW-Url form encoded encoding/decoding
  • File upload both via PUT and POST (multipart/formdata)
  • Some other neat little features....
like image 37
Cumbayah Avatar answered Sep 19 '22 16:09

Cumbayah


You'll want to look up the HttpWebRequest and HttpWebResponse objects. These will be the objects that actually make the HTTP requests.

The request and response will contain XML and JSON in the bodies per ViralHeat's API that you linked to.

like image 22
Jeff Avatar answered Sep 16 '22 16:09

Jeff


This http://www.nuget.org/List/Packages/HttpClient is Microsoft's strategic httpclient moving forward. I Expect to see this library implemented across all of Microsoft's platforms in the near future.

like image 41
Darrel Miller Avatar answered Sep 17 '22 16:09

Darrel Miller