Making and receiving an HTTP request in C#

23,329

Solution 1

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.

Solution 2

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....

Solution 3

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.

Solution 4

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.

Share:
23,329
smohamed
Author by

smohamed

Software Engineer with speciality in Scala language, Microservice architecture, and RESTful API design.

Updated on October 28, 2020

Comments

  • smohamed
    smohamed over 3 years

    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

  • Darrel Miller
    Darrel Miller over 12 years
    At some point this approach is going to bite if you try and use it with XML. You will get a byte order mark as the first few characters and the XML paser will not deserialize it properly.
  • Waihon Yew
    Waihon Yew over 12 years
    @DarrelMiller: Why would the web server return a BOM when there's the Content-Type header to specify the encoding? Even if it does, a simple StartsWith/SubString combo makes it very easy to work around it.
  • Darrel Miller
    Darrel Miller over 12 years
    Sure, it's not too difficult to fix if you know what the problem is, but most people who choose to use webclient.DownloadString usually don't!
  • Waihon Yew
    Waihon Yew over 12 years
    @DarrelMiller: That doesn't answer my first question.
  • smohamed
    smohamed over 12 years
    Thanks alot, it worked :D but it takes about 12 seconds to complete the request and get the answer, is there's any way I can make this faster? I have to make this call +7000 times :)
  • Waihon Yew
    Waihon Yew over 12 years
    @SherifMaherEaid: Can't say without knowing exactly which part of the process took that long.
  • Darrel Miller
    Darrel Miller over 12 years
    @Jon I assume that sometimes people use web servers to serve up XML or XHTML from static text files that contain BOMs.
  • Darrel Miller
    Darrel Miller over 12 years
    @SherifMaherEaid Install Fiddler and use it's performance monitoring tools to determine how long the remote server takes to produce the response and how much time it takes to transfer the result across the wire.
  • Montana Harkin
    Montana Harkin about 12 years
    How do you know it will be implemented across all of Microsoft's platforms? Link?
  • Darrel Miller
    Darrel Miller about 12 years
    @MontanaHarkin I didn't say I did know :-). I said, "I expect". I do know that it has been architected to enable this support. That's why features that are specific to Windows desktop, like WinINetProxy are in the WebRequestHandler and not in the default HttpClientHandler? I also have been told by one of the architects that it will run on WinRT.