1

我需要为 HttpWebRequest 绑定本地 ip 地址(机器有多个 ip)。我创建了委托方法,它被调用并且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 绑定这个?

4

1 回答 1

3

要绑定使用代理的请求,请使用 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 请求上调用代表。

于 2010-03-10T02:56:44.137 回答