Request Web Page in c# spoofing the Host

后端 未结 9 1305
攒了一身酷
攒了一身酷 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)

提交回复
热议问题