如何在HttpWebRequest中设置自定义“主机”标头?

时间:2009-09-20 12:41:16

标签: .net http reflection httpwebrequest host

如何在HttpWebRequest中设置自定义主机头?我知道通常这个类不允许你这样做但是无论如何使用反射或类似的东西而不需要我用TCPClient发送整个数据包?

5 个答案:

答案 0 :(得分:5)

有一种迂回的方法可以做到这一点,如下所述:

http://blogs.msdn.com/feroze_daud/archive/2005/03/31/404328.aspx

但是,下一版本的框架(.NET Framework 4.0)将使其更容易。

http://blogs.msdn.com/ncl/archive/2009/07/20/new-ncl-features-in-net-4-0-beta-2.aspx

希望这有帮助。

答案 1 :(得分:2)

您可以使用此hack,专为在.Net 3.5中解决此问题而设计。

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;


namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://198.252.206.16");

            FieldInfo headersFieldInfo =  request.GetType().GetField("_HttpRequestHeaders", System.Reflection.BindingFlags.NonPublic
                                                    | System.Reflection.BindingFlags.Instance
                                                    | System.Reflection.BindingFlags.GetField);

            CusteredHeaderCollection WssHeaders = new CusteredHeaderCollection("stackoverflow.com");

            headersFieldInfo.SetValue(request, WssHeaders);

            request.Proxy = null;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            StreamReader sr = new StreamReader(response.GetResponseStream());
            string result = sr.ReadToEnd();
            Console.WriteLine(result);
            Console.ReadLine();

        }
        public class CusteredHeaderCollection : WebHeaderCollection
        {
            public bool HostHeaderValueReplaced { get;private  set; }

            public string ClusterUrl { get; private set; }

            public CusteredHeaderCollection(string commonClusterUrl) : base()
            {
                if (string.IsNullOrEmpty("commonClusterUrl"))
                    throw new ArgumentNullException("commonClusterUrl");

                this.ClusterUrl = commonClusterUrl;
            }

            public override string ToString()
            {
                this["Host"] = this.ClusterUrl;
                string tmp =  base.ToString();
                this.HostHeaderValueReplaced = true;

                return tmp;
            }

        }
    }
}

答案 2 :(得分:2)

Necromancing。
对于仍然使用.NET 2.0的用户 事实上,如果你知道如何,这很容易。

问题是,您无法设置主机头,因为框架不允许您在运行时更改该值。 (.net framework 4.0+将允许您覆盖httpwebrequest中的主机)。

下一次尝试将使用反射设置标题,以绕过它,这将允许您更改标题值。但是在运行时,它将使用url的主机部分覆盖此值,这意味着反射将不会带来任何结果。

如果dns-name不存在,坦率地说,这是你想要首先执行此操作的唯一情况,你无法设置它,因为.NET无法解析它,而你无法覆盖.NET DNS解析程序。

但是你可以做的是设置一个与目标服务器完全相同的webproxy。

因此,如果您的服务器IP是28.14.88.71:

public class myweb : System.Net.WebClient
{
    protected override System.Net.WebRequest GetWebRequest(System.Uri address)
    {
        System.Net.WebRequest request = (System.Net.WebRequest)base.GetWebRequest(address);
        //string host = "redmine.nonexistantdomain.com";

        //request.Headers.GetType().InvokeMember("ChangeInternal",
        //    System.Reflection.BindingFlags.NonPublic |
        //    System.Reflection.BindingFlags.Instance |
        //    System.Reflection.BindingFlags.InvokeMethod, null,
        //    request.Headers, new object[] { "Host", host }
        //);

        //server IP and port
        request.Proxy = new System.Net.WebProxy("http://28.14.88.71:80");

        // .NET 4.0 only
        System.Net.HttpWebRequest foo = (System.Net.HttpWebRequest)request;
        //foo.Host = host;

        // The below reflection-based operation is not necessary, 
        // if the server speaks HTTP 1.1 correctly
        // and the firewall doesn't interfere
        // https://yoursunny.com/t/2009/HttpWebRequest-IP/
        System.Reflection.FieldInfo horribleProxyServicePoint = (typeof(System.Net.ServicePoint))
            .GetField("m_ProxyServicePoint", System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance);

        horribleProxyServicePoint.SetValue(foo.ServicePoint, false);
        return foo;



        return request;
    }


    }

瞧,现在

myweb wc = new myweb();
string str = wc.DownloadString("http://redmine.non-existant-domain.com");

如果28.14.88.71是具有基于虚拟名称的托管(基于http-host-header)的网络服务器,则会返回正确的页面。

答案 3 :(得分:0)

您可以使用代理,请参阅我的回答: Request Web Page in c# spoofing the Host

答案 4 :(得分:0)

WebClient允许它。

var client = new WebClient();
client.Headers.Add( "Host", WebHeader );

我无法告诉你为什么。文档明确指出Host是系统头。