Request Web Page in c# spoofing the Host

后端 未结 9 1278
攒了一身酷
攒了一身酷 2020-12-01 06:10

I need to create a request for a web page delivered to our web sites, but I need to be able to set the host header information too. I have tried this using HttpWebRequest,

相关标签:
9条回答
  • 2020-12-01 06:34

    I had a problem where the URL dns I used had several different IP addresses, I wanted to call each address separately using the same dns name in the host - the solution is using a proxy:

    string retVal = "";
                // Can't change the 'Host' header property because .NET protects it
                // HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                // request.Headers.Set(HttpRequestHeader.Host, DEPLOYER_HOST);
                // so we must use a workaround
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy = new WebProxy(ip);
                using (WebResponse response = request.GetResponse())
                {
                    using (TextReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                            retVal += line;
                    }
                }
                return retVal;
    

    Host header is set from 'url' automatically by .NET, and 'ip' contains the actual address of the web server you want to contact (you can use a dns name here too)

    0 讨论(0)
  • 2020-12-01 06:37

    You can use my solution for this problem, it posted here :

    How to set custom "Host" header in HttpWebRequest?

    This can help you to edit host header, and avoid to using proxy and direct socket requests.

    0 讨论(0)
  • 2020-12-01 06:38

    The "Host" header is protected and cannot be modified programmatically. I suppose to work around this, you could try and bind via reflection to the private "InnerCollection" property of the WebRequest object and calling the "Set" ar "Add" method on it to modify the Host header. I haven't tried this, but from a quick look at the source code in Reflector, I think it's easily accomplished. But yeah, binding to private properties of framework objects is not the best way to accomplish things. :) Use only if you MUST.

    edit: Or like the other guy mentions in the linked question, just open up a socket and do a quick "GET" manually. Should be a no brainer, if you don't need to tinker with other stuff, like cookies or whatever else niceties the HttpWebRequest provides.

    0 讨论(0)
提交回复
热议问题