how to change originating IP in HttpWebRequest
According to this, no. You may have to drop down to using Sockets, where I know you can choose the local IP.
EDIT: actually, it seems that it may be possible. HttpWebRequest has a ServicePoint Property, which in turn has BindIPEndPointDelegate, which may be what you're looking for.
Give me a minute, I'm going to whip up an example...
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com");
req.ServicePoint.BindIPEndPointDelegate = delegate(
ServicePoint servicePoint,
IPEndPoint remoteEndPoint,
int retryCount) {
if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) {
return new IPEndPoint(IPAddress.IPv6Any, 0);
} else {
return new IPEndPoint(IPAddress.Any, 0);
}
};
Console.WriteLine(req.GetResponse().ResponseUri);
Basically, the delegate has to return an IPEndPoint. You can pick whatever you want, but if it can't bind to it, it'll call the delegate again, up to int.MAX_VALUE times. That's why I included code to handle IPv6, since IPAddress.Any is IPv4.
If you don't care about IPv6, you can get rid of that. Also, I leave the actual choosing of the IPAddress as an exercise to the reader :)
Change originating IP - circumventing caching of connections
Using HttpWebRequest.ConnectionGroupName
you should be able to get around the ServicePoint connection reuse.
var hr = (HttpWebRequest)WebRequest.Create("http://google.com?");
hr.ConnectionGroupName = hr.RequestUri.ToString(); //Different connection pool per url. Might be better to make this random.
hr.GetResponse();
Alternatively you can just force close the group using ServicePoint.CloseConnectionGroup
. The default group is null.
var hr = (HttpWebRequest)WebRequest.Create("http://google.com");
hr.ServicePoint.CloseConnectionGroup(null);
hr.GetResponse();
How to send httpwebrequests through specific ip address
Taken from this question
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com");
req.ServicePoint.BindIPEndPointDelegate = delegate(
ServicePoint servicePoint,
IPEndPoint remoteEndPoint,
int retryCount) {
if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) {
return new IPEndPoint(IPAddress.IPv6Any, 0);
} else {
return new IPEndPoint(IPAddress.Any, 0);
}
};
Console.WriteLine(req.GetResponse().ResponseUri);
HttpWebRequest to different IP than the domain resolves to
One way to spoof a HTTP host header is to set a proxy to the actual server you'd like the request sent to. Something like
request.Proxy = new WebProxy(string.Format("http://{0}/", hostAddress));
may well work.
Using Client IP address in HttpWebRequest
This is impossible. My answer to the other question was about selecting which IP Address (read: network adapter) to use for a request. However, you cannot invent IP Addresses out of thin air, nor use IP Addresses that are not yours (in a physical, attached-to-this-computer sense).
Now, technically, using Raw Sockets, you can spoof another IP Address in your packets. However, the problem with that is that the return traffic will go to the IP Address you specify, not the one you actually have!
So, my advice is to not pursue this line of thought any further, and find another way to do whatever it is that you are trying to do.
Related Topics
When Is It Ok to Catch an Outofmemoryexception and How to Handle It
C# Help Reading Foreign Characters Using Streamreader
C#: Detecting Which Application Has Focus
Why Doesn't the Xmlserializer Need the Type to Be Marked [Serializable]
Merge 2 Datatables and Store in a New One
How to Get the Subscribers of an Event
Why Does the Linq Cast<> Helper Not Work with the Implicit Cast Operator
Reasons for Why a Winforms Label Does Not Want to Be Transparent
Generate Ef Orderby Expression by String
The Connection Was Not Closed the Connection's Current State Is Open
How to Run Commands on Ssh Server in C#
C# Image.Clone Out of Memory Exception