是否有任何C#等同于Perl的LWP :: UserAgent?

Hai*_*ang 3 c# perl

在我投入的项目中,有一个要求是某些股票的价格将从某个网络界面查询并以某种方式显示.

我知道可以使用像LWP :: UserAgent这样的Perl模块轻松实现需求的"查询"部分.但出于某种原因,已选择C#作为实现Display部分的语言.我不想在这个小项目中添加任何IPC(如套接字,或间接由数据库),所以我的问题是,是否有任何C#等同于Perl的LWP :: UserAgent?

Jus*_*ong 6

您可以使用System.Net.HttpWebRequest对象.

它看起来像这样:

// 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();
}
Run Code Online (Sandbox Code Playgroud)