11

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

smohamed
  • 3,234
  • 4
  • 32
  • 57

5 Answers5

19

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 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. – Darrel Miller Oct 27 '11 at 14:46
  • @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. – Jon Oct 27 '11 at 14:48
  • 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! – Darrel Miller Oct 27 '11 at 14:52
  • @DarrelMiller: That doesn't answer my first question. – Jon Oct 27 '11 at 14:55
  • 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 :) – smohamed Oct 27 '11 at 15:10
  • @SherifMaherEaid: Can't say without knowing exactly which part of the process took that long. – Jon Oct 27 '11 at 15:12
  • @Jon I assume that sometimes people use web servers to serve up XML or XHTML from static text files that contain BOMs. – Darrel Miller Oct 27 '11 at 16:10
  • @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. – Darrel Miller Oct 27 '11 at 16:14
6

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....
Cumbayah
  • 4,415
  • 1
  • 25
  • 32
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.

Jeff
  • 13,943
  • 11
  • 55
  • 103
0

You could implement a WCF REST API : http://www.codeproject.com/KB/WCF/RestServiceAPI.aspx

Francis
  • 3,335
  • 20
  • 46
0

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.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • How do you know it will be implemented across all of Microsoft's platforms? Link? – Montana Harkin Mar 14 '12 at 13:36
  • 1
    @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. – Darrel Miller Mar 14 '12 at 19:57
  • At the time of this comment, "The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore." – emmagras Dec 13 '22 at 17:25
  • @emmagras The HttpClient library is now part of .NET. For .NET 4.5 and earlier you can use this package. – Darrel Miller Jan 07 '23 at 22:23