Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#从URL获取内容获取此错误“;WebRequest不包含GetRespone和…”;_C#_Webrequest - Fatal编程技术网

使用C#从URL获取内容获取此错误“;WebRequest不包含GetRespone和…”;

使用C#从URL获取内容获取此错误“;WebRequest不包含GetRespone和…”;,c#,webrequest,C#,Webrequest,此代码来自Microsoft的文档。我将此代码分别放在控制台应用程序和Windows窗体应用程序中 在控制台应用程序中,出现错误:“WebRequest不包含GetRespone和…”的定义 但在Windows窗体应用程序中,没有错误 我真的不知道为什么会这样。我是C#的初学者,所以这个问题可能很愚蠢。但是我感到很困惑。请给我解释一下。 谢谢大家! 以下是这两种情况的两个屏幕截图: 这是代码 using System; using System.IO; using System.Net;

此代码来自Microsoft的文档。我将此代码分别放在控制台应用程序和Windows窗体应用程序中

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

但在Windows窗体应用程序中,没有错误

我真的不知道为什么会这样。我是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()
也在抱怨,这让我相信一个或多个引用的程序集丢失了。您能否检查项目中的
引用
文件夹,并查看其中是否有黄色警告图标?

您必须使用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();
        }
    }
}
Create将创建WebRequest的派生类,该类具有指定地址的确切实现(HttpWebRequest、FtpWebRequest等),因此必须强制转换到具体实现才能访问具体函数


另外,由于HttpWebRequest.GetResponse返回WebResponse的具体实现,您必须将其强制转换为HttpWebResponse。

谢谢,John。我在并行虚拟机上使用Macbook Pro,VS版本是Enterprise 2017,.net framework是4.5.2。但在我换上windows笔记本电脑之后,代码运行得非常完美。也许问题在于虚拟机。。。这很奇怪。看来我不能只相信虚拟机。。。无论如何,谢谢你的帮助!看来我太乐观了。当我使用Visual Studio 2017时,即使我在Windows笔记本电脑上构建它,错误仍然会显示出来。所以,我认为问题很有可能是Visual Studio 2017…谢谢,Gusman。实际上,该代码来自微软的官方文档,其他一些人已经在运行该代码时没有出现错误。我发现:“我在并行虚拟机上使用Macbook Pro,VS版本是Enterprise 2017,.net framework是4.5.2。但在我换成windows笔记本电脑后,代码运行得很完美。也许问题出在虚拟机上?……这很奇怪。似乎我不能只相信虚拟机……”,谢谢你的帮助!这是我关于StackOverFlow的第一个问题,你给了我很多帮助。谢谢大家!
GetResponse
是在
WebRequest
类中定义的虚拟方法,因此它由所有派生类继承,这些派生类可以由
WebRequest.Create返回。因此,这个答案是不正确的。参考:@JohnM.Wright是的,你是对的。这就是为什么微软的文档使用这段代码作为例子。看起来我太乐观了。当我使用Visual Studio 2017时,即使我在Windows笔记本电脑上构建它,错误仍然会显示出来。所以,我认为问题很有可能是Visual Studio 2017…Gusman是正确的。你的代码已经过时了。请参阅本页底部的文档和代码示例。