Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.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# WebRequest和协议无关URL_C#_Asp.net_Httpwebrequest - Fatal编程技术网

C# WebRequest和协议无关URL

C# WebRequest和协议无关URL,c#,asp.net,httpwebrequest,C#,Asp.net,Httpwebrequest,在web应用程序中,我必须首先检查图像是否存在,然后显示此图像或虚拟图像 我使用以下代码,它适用于URL,如: “http://www.somedomain.com/niceimage.png” “https://www.somedomain.com/niceimage.png” 在某些地方,使用与协议无关的URL调用该方法,如“//www.somedomain.com/niceimage.png” 这类URL会引发异常: System.InvalidCastException:无法强制转换

在web应用程序中,我必须首先检查图像是否存在,然后显示此图像或虚拟图像

我使用以下代码,它适用于URL,如:

  • http://www.somedomain.com/niceimage.png”
  • https://www.somedomain.com/niceimage.png”
在某些地方,使用与协议无关的URL调用该方法,如
“//www.somedomain.com/niceimage.png”

这类URL会引发异常:

System.InvalidCastException:无法强制转换类型为的对象
“System.Net.FileWebRequest”以键入“System.Net.HttpWebRequest”


是否有一种方法可以使用与协议无关的url,而不只是将http:前置到url?

浏览器使用当前协议解析与协议无关的url,并用于避免从HTTPS页面发出http请求

在服务器上执行的代码实际上没有“当前协议”的概念。虽然ASP.NET可以确定当前请求是通过HTTP还是HTTPS发出的,但是
WebRequest
类并不限于ASP.NET应用程序,因此它们不能依赖于此


您需要指定协议。您是使用HTTP还是HTTPS将取决于您是否担心第三方窃听您的服务器与“www.somedomain.com”之间的连接。

关于两步流程,请检查HTTP版本,如果不存在,请检查HTTPS。我很快就拼凑出了一个基本的例子,说明这是如何工作的,但是我无法正确地测试和检查它,所以它可能需要一些整理/重构

public virtual bool WebResourceExists(string url)
{
    WebHeaderCollection headers = null;
    WebResponse response = null;
    try
    {
        if (url.StartsWith(@"//") {
           url = "http:";
        }
        WebRequest request = WebRequest.Create(url);
        request.Method = "HEAD";
        response = request.GetResponse();
        headers = response.Headers;
        bool result = int.Parse(headers["Content-Length"]) > 0;
        return result;
    }
    catch (System.Net.WebException)
    {
        if (url.StartsWith(@"http://") {
           url = url.Replace("http://","https://");
        } else {
           return false;
        }
        try {
            WebRequest request = WebRequest.Create(url);
            request.Method = "HEAD";
            response = request.GetResponse();
           headers = response.Headers;
           bool result = int.Parse(headers["Content-Length"]) > 0;
           return result;
       }
       catch (System.Net.WebException)
       {
            return false;
       }
    }
    catch (Exception e)
    {
        _log.Error(e);
        return false;
    }
    finally
    {
        if (response != null)
        {
            response.Close();
        }
    }
}

您将
WebRequest
转换为
HttpWebRequest
WebResponse
转换为
HttpWebResponse
是否有原因?没有原因。我更新了代码示例。当不涉及对HttpWebRequest的转换时,行为不会改变。
public virtual bool WebResourceExists(string url)
{
    WebHeaderCollection headers = null;
    WebResponse response = null;
    try
    {
        if (url.StartsWith(@"//") {
           url = "http:";
        }
        WebRequest request = WebRequest.Create(url);
        request.Method = "HEAD";
        response = request.GetResponse();
        headers = response.Headers;
        bool result = int.Parse(headers["Content-Length"]) > 0;
        return result;
    }
    catch (System.Net.WebException)
    {
        if (url.StartsWith(@"http://") {
           url = url.Replace("http://","https://");
        } else {
           return false;
        }
        try {
            WebRequest request = WebRequest.Create(url);
            request.Method = "HEAD";
            response = request.GetResponse();
           headers = response.Headers;
           bool result = int.Parse(headers["Content-Length"]) > 0;
           return result;
       }
       catch (System.Net.WebException)
       {
            return false;
       }
    }
    catch (Exception e)
    {
        _log.Error(e);
        return false;
    }
    finally
    {
        if (response != null)
        {
            response.Close();
        }
    }
}