.NET:WebBrowser、WebClient、WebRequest、HTTPWebRequest...哎呀!

发布于 2024-08-12 02:35:30 字数 251 浏览 5 评论 0原文

在System.Net命名空间中,有非常多具有相似名称的不同类,例如:

  • WebBrowser和WebClient
  • WebRequest和HTTPWebRequest
  • WebResponse和HTTPWebResponse

这些是我很好奇的主要类。

每个人的职能是什么?它们之间有何不同?

另外,在什么情况下您会使用哪个?

In the System.Net namespace, there are very many different classes with similar names, such as:

  • WebBrowser and WebClient
  • WebRequest and HTTPWebRequest
  • WebResponse and HTTPWebResponse

Those are the main ones I'm curious about.

What is each one's function? How are they different from one another?

Also, in what cases would you use which?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久随 2024-08-19 02:35:30

WebBrowser 实际上位于 System.Windows.Forms 命名空间中,是一个可以添加到表单的可视控件。它主要是 Internet Explorer 浏览器 (MSHTML) 的包装。它允许您以编程方式轻松显示网页并与网页交互。您可以调用传递 Web URL 的 Navigate 方法,等待它完成下载和显示,然后使用它提供的对象模型与页面交互。

HttpWebRequest 是一个具体类,允许您在代码中通过 HTTP 请求任何类型的文件。您通常以字节流的形式接收它。之后您如何处理它取决于您的应用程序。

HttpWebResponse 允许您处理之前使用 HttpWebRequest 请求的 Web 服务器的响应。

WebRequestWebResponse 是 HttpWebRequest 和 HttpWebResponse 继承的抽象基类。您无法直接创建这些。从这些类继承的其他类包括 Ftp 和 File 类。

WebClient 我一直将其视为一个很好的帮助器类,它提供了更简单的方法,例如从 Web url 下载或上传文件。 (例如 DownloadFile 和 DownloadString 方法)。我听说它实际上在幕后使用 HttpWebRequest / HttpWebResponse 来实现某些方法。

如果您需要对 Web 请求和响应进行更细粒度的控制,则 HttpWebRequest / HttpWebResponse 可能是最佳选择。否则,WebClient 通常更简单并且可以完成这项工作。

WebBrowser is actually in the System.Windows.Forms namespace and is a visual control that you can add to a form. It is primarily a wrapper around the Internet Explorer browser (MSHTML). It allows you to easily display and interact programmatically with a web page. You call the Navigate method passing a web URL, wait for it to complete downloading and display and then interact with the page using the object model it provides.

HttpWebRequest is a concrete class that allows you to request in code any sort of file over HTTP. You usually receive it as a stream of bytes. What you do with it after that is up to your application.

HttpWebResponse allows you to process the response from a web server that was previously requested using HttpWebRequest.

WebRequest and WebResponse are the abstract base classes that the HttpWebRequest and HttpWebResponse inherit from. You can't create these directly. Other classes that inherit from these include Ftp and File classes.

WebClient I have always seen as a nice helper class that provides simpler ways to, for example, download or upload a file from a web url. (eg DownloadFile and DownloadString methods). I have heard that it actually uses HttpWebRequest / HttpWebResponse behind the scenes for certain methods.

If you need more fine grained control over web requests and responses, HttpWebRequest / HttpWebResponse are probably the way to go. Otherwise WebClient is generally simpler and will do the job.

夜雨飘雪 2024-08-19 02:35:30

我不知道有什么 System.Net.WebBrowser,但 WebClient 基本上是一个类,可以让您轻松地将文件(包括 html 页面)从 Web 下载到内存中,甚至直接下载到文件中。基本代码示例如下所示:

string html;
using (var wc = new WebClient())
{
    html = wc.DownloadString("http://stackoverflow.com/questions/1780679/");
}

您可以使用 WebClient 做很多事情,但有一些限制。如果您需要进行一些严重的网络抓取,则需要获得较低的级别。这就是 HttpWebRequest/HttpWebResponse 的用武之地。您可以使用它们以任何顺序发送普通 Web 浏览器可能发送的任何请求。例如,您可能需要先通过网站进行身份验证,然后才能请求您真正想要的页面,而 WebClient 可能无法做到这一点。 HttpWebRequest 将会。

现在,还有另一种选择。 System.Windows.Forms.WebBrowser 是一个设计用于放置在表单上的控件。它基本上封装了 Internet Explorer 中使用的引擎,以提供 Web 浏览器的所有功能。您需要小心地将其用于一般抓取:它不可移植(对单声道不利),使用大量资源,具有与运行完整浏览器类似的安全问题,并且具有副作用,例如可能泄漏弹出窗口。该控件最好以连接到特定已知 Web 资源的形式使用。例如,您可能有一个可供销售的 Windows 窗体应用程序,以及一个可供下载的 Web 应用程序。您可以提供一个 WebBrowser 控件,该控件显示此网站上专门用于在您的应用程序中查看的几个页面,允许用户购买应用程序内升级。

I don't know of any System.Net.WebBrowser, but WebClient is basically a class that lets you easily download files (including html pages) from the web into memory or even directly to file. A basic code sample looks like this:

string html;
using (var wc = new WebClient())
{
    html = wc.DownloadString("http://stackoverflow.com/questions/1780679/");
}

You can do a lot with WebClient, but there are some limitations. If you need to do some serious web scraping, you'll need to get lower level. That's where the HttpWebRequest/HttpWebResponse come in. You can use them to send any request a normal web browser might send, in any sequence. For example, you may need to authenticate with a web site before you can request the page you really want, and WebClient might not be able to do that. HttpWebRequest will.

Now, there is one other option. System.Windows.Forms.WebBrowser is a control designed to place on a form. It basically wraps the engine used in Internet Explorer to provide all the capabilities of a web browser. You need to be careful using this for general scraping: it's not portable (bad for mono), uses a lot of resources, has similar security issues as running a full browser, and has side-effects such as potentially leaking popup windows. The control is best used in a form to connect to a specific known web resource. For example, you may have a Windows Forms app for sale, and web app where you sell it for download. You might provide a WebBrowser control that shows a few pages on this web site specifically intended for view in your app that allows users to purchase in-app upgrades.

残月升风 2024-08-19 02:35:30

WebRequest 和 WebResponse 是抽象类。 HTTPWebRequest 和 HTTPWebResponse 是它们的实现。

WebRequest and WebResponse are abstract classes. HTTPWebRequest and HTTPWebResponse are implementations of them.

迟到的我 2024-08-19 02:35:30

WebClient 是获取 HTML 页面的一种非常巧妙的方法。
这是下载响应字符串的代码片段。

   string getHtmlPageUsingWC(string strQuery, System.Net.WebProxy proxy = null)
    {
        string strResponse = String.Empty;
        using (WebClient wc = new WebClient())
        {
            wc.Encoding = Encoding.UTF8;
            IWebProxy wp = WebRequest.DefaultWebProxy;
            wp.Credentials = CredentialCache.DefaultCredentials;
            wc.Proxy = wp;
            wc.Headers.Add("Accept-Language:en");

            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("q", strQuery);

            wc.QueryString.Add(nvc);

            try
            {
                strResponse = wc.DownloadString(m_strURL);
            }
            catch (Exception ex)
            {
                strResponse = "Request Declined: " + ex.Message;
                Console.WriteLine(ex.Message);
            }
        }

        return strResponse;
    }

WebClient is quite a neat way to fetch the HTML page.
Here is the code snippet to download a response string.

   string getHtmlPageUsingWC(string strQuery, System.Net.WebProxy proxy = null)
    {
        string strResponse = String.Empty;
        using (WebClient wc = new WebClient())
        {
            wc.Encoding = Encoding.UTF8;
            IWebProxy wp = WebRequest.DefaultWebProxy;
            wp.Credentials = CredentialCache.DefaultCredentials;
            wc.Proxy = wp;
            wc.Headers.Add("Accept-Language:en");

            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("q", strQuery);

            wc.QueryString.Add(nvc);

            try
            {
                strResponse = wc.DownloadString(m_strURL);
            }
            catch (Exception ex)
            {
                strResponse = "Request Declined: " + ex.Message;
                Console.WriteLine(ex.Message);
            }
        }

        return strResponse;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文