My .net app uses WebClient to access files on the internet and my machine has multiple IP addresses. Is there a way to programatically select which IP to use instead of the first IP when making outbound requests? (doesn't have to be WebClient)
One possibility to achieve this is to use the ServicePoint.BindIPEndPointDelegate event and specify which IP address to use.
Example:
var uri = new Uri("YOUR URI");
var servicePoint = ServicePointManager.FindServicePoint(uri);
servicePoint.BindIPEndPointDelegate = (sp, remoteEndPoint, retryCount) =>
{
var address = IPAddress.Parse("PUT YOUR DESIRED IP HERE");
return new IPEndPoint(address, 0);
};
Now try making the HTTP request to the corresponding url.
Alternatively if you are using an HttpWebrequest you could assign this per delegate per request:
var request = (HttpWebRequest)WebRequest.Create("YOUR URI");
request.ServicePoint.BindIPEndPointDelegate = (sp, remoteEndPoint, retryCount) =>
{
var address = IPAddress.Parse("PUT YOUR DESIRED IP HERE");
return new IPEndPoint(address, 0);
};
using (var response = request.GetResponse())
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With