使用 C# 从 URL 获取内容后出现此错误 "WebRequest does not contain a definition for GetRespone and ..."

Using C# to get content from URL followed by this error "WebRequest does not contain a definition for GetRespone and ..."

这段代码来自微软的文档。我将此代码分别放在控制台应用程序和 Windows 表单应用程序中。

在控制台应用程序中,出现错误:“WebRequest 不包含 GetRespone 和……的定义”

但是在Windows Form app中,没有报错。

我真的不知道为什么会这样。我是 C# 的初学者,所以这个问题可能很愚蠢。但我感到很困惑。请给我解释一下。 谢谢!

下面是这两种情况的两个截图:

enter image description here

enter image description here

这是代码。

using System;
using System.IO;
using System.Net;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a request for the URL.   
            WebRequest request = WebRequest.Create(
              "http://www.contoso.com/default.html");
            // If required by the server, set the credentials.  
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.  
            WebResponse response = request.GetResponse();
            // Display the status.  
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.  
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.  
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.  
            string responseFromServer = reader.ReadToEnd();
            // Display the content.  
            Console.WriteLine(responseFromServer);
            // Clean up the streams and the response.  
            reader.Close();
            response.Close();
        }
    }
}

下面的步骤让我成功了

  1. 新解决方案 - Visual Studio 中的 Visual C# 控制台应用程序。将您的项目命名为 "ConsoleApp1".
  2. 从 Web 复制并粘贴主函数内的代码到新生成的主函数。
  3. 在下面添加 using 语句
using System.Net;
using System.IO;
  1. 运行 按 "F5"。它是成功的。但是 window 一完成就关闭了。
  2. 为了让您看到输出结果,在“}”语句处打断点,避免window关闭。或者,您可以添加以下作为最后一个 C# 语句。然后你需要点击任意键关闭控制台window.

    Console.ReadKey();

输出,我得到:

OK
<html><head><title>Microsoft Corporation</title><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"></meta><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><meta name="SearchTitle" content="Microsoft.com" scheme=""></meta><meta name="Description" content="Get product information, support, and news from Microsoft." scheme=""></meta><meta name="Title" content="Microsoft.com Home Page" scheme=""></meta><meta name="Keywords" content="Microsoft, product, support, help, training, Office, Windows, software, download, trial, preview, demo,  business, security, update, free, computer, PC, server, search, download, install, news" scheme=""></meta><meta name="SearchDescription" content="Microsoft.com Homepage" scheme=""></meta></head><body><p>Your current User-Agent string appears to be from an automated process, if this is incorrect, please click this link:<a href="http://www.microsoft.com/en/us/default.aspx?redir=true">United States English Microsoft Homepage</a></p></body></html>