How to specify server IP in HttpWebRequest
I was writing an HTTP proxy during last weeks. I need to address the origin server with a custom IP address, but I should keep the "Host" header.
I googled about "HttpWebRequest set server IP", "WebRequest modify Host", etc. I read through several posts, and realized:
- When HttpWebRequest is created, the "Host" header is automatically set to the "host:port" portion of the Uri.
- Attempt to modify HttpWebRequest.Headers["Host"] will throw an exception.
- The server IP address of the HTTP Request is determined by the host portion of the Uri, and is automatically resolved against the DNS server.
- When HttpWebRequest.Proxy is set to use a proxy, the server IP address is determined by resolving the host of Proxy property. I can set the origin server as the proxy, this is called "Proxy hack".
- When using a proxy, the HTTP Request-Line looks like "GET http://host/ HTTP/1.1" rather than "GET / HTTP/1.1". Every HTTP/1.1 compliant origin server should accept this, but firewalls may think it's a "proxy request" and block it, and some non-compliant servers does not accept it.
SOLUTION
My purpose is: make HttpWebRequest send its request to a custom IP address, but leave the Request-Line as "GET / HTTP/1.1".