views:

112

answers:

1

I need to bind the local ip address for an HttpWebRequest (machine has multiple ips). I create the delegate method, and this is called and the ip is bound for requests without a proxy, but once i add proxy details to the request, the callback never occurs

How can i bind the outgoing ip address for HttpWebRequests that use a proxy?

    static void MakeRequest(string url, WebProxy myProxy)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
        request.Proxy = myProxy;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    }
    public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
    {
        // not called when proxy is set
        Console.WriteLine("BindIPEndpoint called");
        return new IPEndPoint(IPAddress.Parse("192.168.1.58"), 5000);
    } 

Is there some other way to bind this for https?

A: 

To bind a request that uses a proxy, use ServicePointManager.FindServicePoint;

static void MakeRequest(string url, WebProxy myProxy)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Proxy = myProxy;
    ServicePoint sp = ServicePointManager.FindServicePoint(new Uri(url), myProxy);
    sp.BindIpEndPointDelegate = new BindIpEndPoint(BindIpEndPointCallback);
    HttpWebResponse = (HttpWebResponse)request.GetResponse();
}

Works for http requests, unfortunately the delegate is still not called on https requests.

staff0rd