Using BindIPEndPointDelegate to specify the source port doesn't seem to work

2.2k views Asked by At

I've searched all over on how to use BindIPEndPointDelegate to change the source port of my HttpWebRequest. I have found multiple posts on this:

Here and Here

However, It doesn't seem to be working for me. I found the same issue with this guy: Define Outgoing/Calling Port for SOAP Web Service in Visual Studio 2010

Basically, I want to change my source port when connecting to a webpage. BindIPEndPointDelegate is pretty straigh forward.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
 req.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEp, retryCount) =>
             {
                 return new IPEndPoint(IPAddress.Any, 8086);
             };

When I look at the connections with netstat -n, I expect to see myipaddress:8086 as the source and the destination IP:80. What I see in netstat is that it is creating a loopback (127.0.0.1:8086) with my specified port. See screen shot of netstat -n

Since this is my first post, I can't post images. So the image can be found here

Why does BindIPEndPointDelegate create a loop back connection and how can I get my WebRequest to behave like the browser with my specified port?

I'm on a win7 Pro box, No local firewall, Not going through a proxy.

1

There are 1 answers

0
Keith Morgan On

I had a similar problem today. I selected a particular source port in the BindIPEndPointDelegate, but with WireShark I always saw a different source port than what I selected. Then I discovered that I was implicitly using a Proxy by using the Fiddler2 Web Debugger. Once I turned off Fiddler, then I started seeing the source port numbers in WireShark that I requested in the Bind delegate.

The upshot is that if you have a proxy anywhere you might see this same behavior.