首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(.NET) ServicePoint.IPEndPointDelegate不使用代理回调?

(.NET) ServicePoint.IPEndPointDelegate不使用代理回调?
EN

Stack Overflow用户
提问于 2010-03-05 13:02:37
回答 1查看 1.4K关注 0票数 1

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

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

代码语言:javascript
复制
    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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-10 10:56:44

要绑定使用代理的请求,请使用ServicePointManager.FindServicePoint;

代码语言:javascript
复制
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请求上仍然不调用委托。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2384673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档