To send an HTTP request in C#, you can use either the HttpWebRequest
or HttpClient
class. Here's an example of each:
HttpWebRequest
:main.cs490 chars19 lines
In this example, we first create an instance of the HttpWebRequest
class by passing in the URL we want to request. We then use the GetResponse
method to actually send the request and get the response. Finally, we read the response stream and output the response text to the console.
HttpClient
:main.cs390 chars16 lines
In this example, we create an instance of the HttpClient
class, and then use the GetAsync
method to send an HTTP GET request to the specified URL. We then read the content of the response and output it to the console.
Note that in this example, we're using the await
keyword to asynchronously wait for the response to be returned. This allows us to make the HTTP request without blocking the calling thread.
gistlibby LogSnag