环境:本地测试用的http协议,部署到生产环境的协议是https

用Response.Redirt跳转页面的时候会发生两种协议之间跳转的问题。

为了解决这个问题我想到首先获得访问页面是使用上面两个协议的哪一种

获取协议方法

  protected string CheckHttps()
    {
        int loop1;
        string result = "";
        NameValueCollection coll = Request.ServerVariables;
        string[] arr1 = coll.AllKeys;
        for (loop1 = 0; loop1 < arr1.Length; loop1++)
        {
            if (arr1[loop1] == "HTTPS") { string[] arr2 = coll.GetValues(arr1[loop1]); result = Server.HtmlEncode(arr2[0]); return result; }
        }
        return result;
    }

获取网站的域名的方法

protected string GeHTTP_HOST()
    {
        int loop1;
        string result = "";
        NameValueCollection coll = Request.ServerVariables;
        string[] arr1 = coll.AllKeys;
        for (loop1 = 0; loop1 < arr1.Length; loop1++)
        {
            if (arr1[loop1] == "HTTP_HOST") { string[] arr2 = coll.GetValues(arr1[loop1]); result = Server.HtmlEncode(arr2[0]); return result; }
        }
        return result;
    }

 最后在在跳转的时候进行判断

  string url = string.Empty;
        if (CheckHttps() == "on" || CheckHttps() == "")
        {
            url = "http://" + GeHTTP_HOST() + "/b2badmin/Export2010.aspx?op=SR";
        }
       if (CheckHttps() == "off" )
        {
            url = "https://" + GeHTTP_HOST() + "/b2badmin/Export2010.aspx?op=SR";
        }
        Response.Redirect(url);