Is there any C # equivalent to Perl LWP :: UserAgent?

In the project in which I am involved, there is a requirement that the price of certain stocks will be requested from some web interface and displayed in some way.

I know that part of the request can be easily implemented using a Perl module such as LWP :: UserAgent. But for some reason, C # was chosen as the language for implementing the Display part. I do not want to add any IPC (for example, socket or indirectly through the database) to this tiny project, so my question is, is there any C # equivalent to Perl LWP :: UserAgent?

+3
source share
3 answers

You can use the System.Net.HttpWebRequest object.

:

// Setup the HTTP request.
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");

// This is optional, I'm just demoing this because of the comments receaved.
httpWebRequest.UserAgent = "My Web Crawler"; 

// Send the HTTP request and get the response.
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
    // Get the HTML from the httpWebResponse...
    Stream responseStream = httpWebResponse.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    string html = reader.ReadToEnd();
}
+6

If you just want to get data from the Internet, you can use the WebClient class . This seems pretty good for quick queries.

+2
source

Source: https://habr.com/ru/post/1726028/


All Articles