Reputation: 8903
I need to call the same API many times, but with different IP's based on certain parameters.
I've implemented the code in this SO question: how to change originating IP in HttpWebRequest
This worked well when I needed to specify a single IP for the API I am calling, but now I need to use from several IPs based on my requirements, and this method no longer seems to work. The API URL seems to be cached and subsequent calls will use that IP, instead of the specific one I wish to set on every call.
I need a method that will let me very specifically choose the source IP for each request.
Adding:
If I split my application into two separate apps, each one using its own source ip, would this be a brute force approach to force it to work? in other words, is the caching performed only per process? (I assume this is so).
Upvotes: 2
Views: 329
Reputation: 10411
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();
Upvotes: 1
Reputation: 5160
Here is a quick solution you can try without reflecting upon the underlying libraries:
Put some random junk on the end of your target URL e.g.
http://api.address.com/restful/get?param1=a¶m2=b&junk=13929348398
You can generate a guid, random number, nanotime, etc. This approach works great for preventing caching of responses -- it should work well in this case also. Do you think this would work for you? Assuming the API ignores that last parameter...
Better yet instead of a random number, you could append a hash of your source IP to actually take advantage of the caching behavior your observing.
Upvotes: 0