繁体   English   中英

CodeGo.net>没有HTTP或HTTPS创建HttpWebRequest

[英]c# - Create HttpWebRequest without http or https

我想要使​​用c#的X-Frame-Options。 为此,我正在创建以下内容。

  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(System.Uri.UnescapeDataString(url));
            string frameSupport = string.Empty;
            using (WebResponse webResponse = request.GetResponse())
                frameSupport = webResponse.Headers["X-Frame-Options"];

但是,当我传递不带http或https的url时,出现以下问题。

Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.HttpWebRequest'.

如果使用HttpWebRequest.Create ,则实际上是在调用WebRequest.Create (因为HttpWebRequest扩展了WebRequest )。
因此,该方法不仅适用于http请求,而且还尝试根据uri方案获取请求的实际类型。
如果省略http(s)方案,它将无法知道您正在执行哪种类型的请求,因此,在这种情况下,它将假定它是FileWebRequest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM