GeekZilla
Specifying an originator IP Address on a WebService Request using ServicePoint and HttpWebRequest
If you're running multiple IP Addresses on the same NIC in Windows 2003 server and you need a webservice call to originate from a certain one, you'll probably have found the same thing I did. .net uses the default machine IP address.
I read some posts about specifying only the IP Address you wanted the request to originate from in IIS 6.0 but that didn't help me at all.. I guess this was because mine were on the same NIC.
It took me a while to come up with this solution and I lept for joy when it worked, and made Paul Marshall jump out of his skin.
Create a wrapper for your the Web Service you want to call
The following code shows my wrapped request to a What's My Ip service:
public class SoapServicePointIPWrapper : com.xignite.preview.XigniteHelp { private string servicePointIP; public string ServicePointIP { get { return servicePointIP; } set { servicePointIP = value; } } protected override WebRequest GetWebRequest(Uri uri) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri); request.ServicePoint.BindIPEndPointDelegate = new System.Net.BindIPEndPoint(BindIPEndPointCallback); return request; } public delegate IPEndPoint BindIPEndPoint(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount); private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount) { if (retryCount < 3) return new IPEndPoint(IPAddress.Parse(servicePointIP), 0); else return new IPEndPoint(IPAddress.Any, 0); } }
I'd really like to create a Generic wrapper that didn't need to inherit the 2rd party service.. Perhaps using Generics??
Implementation
You can see below, I'm specifying the IP address I want the request to originate from. If the IP address does not exist, it will use the machine default.
SoapServicePointIPWrapper theService = new SoapServicePointIPWrapper(); theService.ServicePointIP = "192.168.1.5"; //your IP com.xignite.preview.RequestInformation resp = theService.WhatsMyIP(); XMLSerializer<com.xignite.preview.RequestInformation> ser = new XMLSerializer<com.xignite.preview.RequestInformation>();
I hope you found this article in time
Paul is the COO of kwiboo ltd and has more than 20 years IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services.
Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.
Comments
sobelito
said:
you are awesome. this is exactly what I have been seeking!