Alexander Anikin's blog

My personal blog

Making a Request to WebService in Visual Studio from Specific IP address

leave a comment »

Hey all  –

Task:

1. I have a web-service refference in my project. I update it time to time. It’s OK.

2. I have several network adapters in my system.

The main thing – I need to call a WebMethod refferenced in my project from specific IP.

Solution:

As you can see – VS generating class for Web Service Refference and it’s partial. So you can override  some very useful methods. In my case I should override GetWebRequest and change it’s properties. For changing EndPoint I should use ServicePoint.BindIPEndPointDelegate.

Example:


public IPEndPoint BindIpEndPointCallbackForIP(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
return new IPEndPoint(_currentLocalIp, 0);
}

protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request = (HttpWebRequest) base.GetWebRequest(uri);
request.Timeout = 10000;
if (_currentLocalIp != null)
{
request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIpEndPointCallbackForIP);
}
return request;
}

I’m trying to make end-point (string in bold) with specific port (ex. 443 or 80), but it fails (why?). And in my case it doesn’t work with IPv6.

Advertisements

Written by Alex Anikin

February 13, 2012 at 2:50 pm

Posted in .Net, C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: