(.NET)ServicePoint.IPEndPointDelegate不与代理回调?

时间:2010-03-05 05:02:37

标签: .net binding proxy ip system.net

我需要为HttpWebRequest绑定本地IP地址(机器有多个ips)。我创建了委托方法,这被调用,并且ip被绑定到没有代理的请求,但是一旦我向请求添加代理详细信息,回调就不会发生

如何为使用代理的HttpWebRequests绑定传出的ip地址?

    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);
    } 

是否有其他方法可以将此绑定为https?

1 个答案:

答案 0 :(得分:2)

要绑定使用代理的请求,请使用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();
}

适用于http请求,遗憾的是,代理仍未在https请求上调用。