【问题标题】:C# static member `System.Net.WebRequest.Create(string)'C# 静态成员 `System.Net.WebRequest.Create(string)'
【发布时间】:2021-08-06 07:50:38
【问题描述】:

错误 CS0176:无法使用实例引用访问静态成员 `System.Net.WebRequest.Create(string)',而是使用类型名称对其进行限定

为什么这段代码不起作用

using System;
using System.Net;
using System.Net.Http;


namespace main
{
    class firstreq
    {
        public static void Main(string[] args)
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://192.168.240.1");  
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                HttpWebRequest webRequest2 = (HttpWebRequest)webRequest2.Create("https://192.168.240.1/getname");  
                HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
            }  
        }
    }
}

我试过去除静电,但它不起作用,任何解决方案都会很酷,谢谢

【问题讨论】:

  • 在HttpWebRequest webRequest2 = (HttpWebRequest)webRequest2.Create("https://192.168.240.1/getname"); 中,第二个webRequest2 应该是WebRequest。

标签: c# methods static


【解决方案1】:

WebRequest.Create 是一个静态方法,因此只能在类型名称 (WebRequest) 上引用,而不能通过类的实例。

尝试做:

 HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create("https://192.168.240.1/getname");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多