使用 C# 从 URL 获取内容时出现此错误“WebRequest 不包含 GetRespone 和……的定义”

Using C# get content from URL get this error “WebRequest does not contain a definition for GetRespone and …”

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

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

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

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

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

这是代码。

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:

我是并行虚拟机用的Macbook Pro,VS版本是Enterprise 2017,.net framework是4.5.2。

但在我换到 windows 笔记本电脑后,代码 运行 完美无缺。也许问题出在虚拟机上? ……很奇怪。看来我不能只相信虚拟机...无论如何,感谢您的帮助!

更新 2:

看来是我太乐观了。当我使用 Visual Studio 2017 时,即使我在 Windows 笔记本电脑上构建它,错误仍然显示。所以,我认为问题很有可能是 Visual Studio 2017...

看起来 reader.Close() 也在抱怨,这使我相信您引用的一个或多个程序集丢失了。您可以检查项目中的 References 文件夹,看看是否有黄色警告图标?

您必须使用 HttpWebRequest 和 HttpWebResponse,而不是 WebRequest:

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a request for the URL.   
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
              "http://www.contoso.com/default.html");
            // If required by the server, set the credentials.  
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.  
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // 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();
        }
    }
}

WebRequest.Create 将为指定地址创建一个具有确切实现(HttpWebRequest、FtpWebRequest 等)的 WebRequest 的派生 class,因此您必须强制转换为具体实现才能访问具体函数。

此外,作为 HttpWebRequest.GetResponse returns WebResponse 的具体实现,您必须强制转换它,在本例中为 HttpWebResponse。