How to set custom "Host" header in HttpWebRequest?

31,670

Solution 1

There is a roundabout way to do this, as described here:

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

However, the next version of the framework (.NET Framework 4.0) will make it easier.

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

Hope this helps.

Solution 2

Necromancing.
For those still on .NET 2.0
It is in fact quite easy, if you know how.

Problem is, you can't set the host header, because the framework won't let you change the value at runtime. (.net framework 4.0+ will let you override host in a httpwebrequest).

Next attempt will be setting the header with reflection, to get around it, which will let you change the header value. But at runtime, it will overwrite this value with the host part of the url, which means reflection will bring you nothing.

If the dns-name doesn't exist, which is quite frankly the only case in which you want to do this in the first place, you can't set it, because .NET can't resolve it, and you can't override the .NET DNS resolver.

But what you can do, is setting a webproxy with the exact same IP as the destination server.

So, if your server IP is 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;
    }


    }

and voila, now

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

and you get the correct page back, if 28.14.88.71 is a webserver with virtual name-based hosting (based on http-host-header).

Solution 3

You can use this hack, designed for solve this problem in .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;
            }

        }
    }
}

Solution 4

WebClient allows it.

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

I couldn't tell you why. The documentation clearly states that Host is a system header.

Share:
31,670
Djonatas Tenfen
Author by

Djonatas Tenfen

SO Domination is so close! Ni hahahahahaha

Updated on February 08, 2020

Comments

  • Djonatas Tenfen
    Djonatas Tenfen about 4 years

    How can I set a custom Host header in HttpWebRequest? I know that normally this class doesn't allow you to do so but is there anyway to use reflection or something like that without actually need me to send the whole packet with TCPClient?

  • Djonatas Tenfen
    Djonatas Tenfen over 14 years
    I've seen that page although there are lots of problems in that workaround beside of that it's a really dirty workaround :)
  • Stefan Steiger
    Stefan Steiger over 7 years
    That's because it doesn't work - exception host header cannot be directly changed.
  • Admin
    Admin over 7 years
    Nice, for .NET 2.0. Obviously, this won't work if you actually need to use a proxy-server ;) Just upgrade to .NET 4.5 already - WebRequest and WebClient are both deprecated anyway.
  • Mikhail
    Mikhail almost 7 years
    Not works. ArgumentException: This header must be modified with the appropiate property.
  • Patrick
    Patrick almost 7 years
    It literally works. As I said, running in production. System.Net.WebClient. Target 3.5