首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >硬代码IPEndpoint在ASP.Net中使用ServicePoint.BindIPEndPointDelegate提供异常

硬代码IPEndpoint在ASP.Net中使用ServicePoint.BindIPEndPointDelegate提供异常
EN

Stack Overflow用户
提问于 2012-02-15 16:42:02
回答 2查看 889关注 0票数 0

我正试图为我的webRequest绑定我的出站IP

代码语言:javascript
复制
HttpWebRequest reqhttp = (HttpWebRequest)req;
reqhttp.ServicePoint.BindIPEndPointDelegate = new System.Net.BindIPEndPoint(BindIPEndPointCallback);
reqhttp.Credentials = null;
reqhttp.AuthenticationLevel = AuthenticationLevel.None;
reqhttp.Method = "POST";
reqhttp.ContentLength = send.Length;
reqhttp.ContentType = "text/xml";

Stream dataStream = reqhttp.GetRequestStream();
dataStream.Write(send, 0, send.Length);
dataStream.Close();

 public delegate IPEndPoint BindIPEndPoint(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount);

    private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
    {
        return new IPEndPoint(IPAddress.Parse("111.111.11.11"), 0); //bind to a specific ip address on your server
    }

由于某些原因,当我这样做时,它会抛出一个错误。

如果未能执行此行

代码语言:javascript
复制
Stream dataStream = reqhttp.GetRequestStream();

现有连接被远程主机强制关闭

我不明白这里出了什么问题。

任何一个人都能帮助理解这段代码的错误并解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2013-09-04 01:11:27

GetRequestStream()方法将首先触发BindIPEndPointDelegat,然后尝试连接到远程服务器。如果绑定到不存在的本地终结点,或者远程服务器不可用,则会得到异常。

票数 1
EN

Stack Overflow用户

发布于 2012-02-15 17:02:03

试试像这样的东西

代码语言:javascript
复制
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myIP);
request.Proxy = myProxy;
ServicePoint sp = ServicePointManager.FindServicePoint(new Uri(myIP), myProxy);
sp.BindIpEndPointDelegate = new BindIpEndPoint(BindIpEndPointCallback);
HttpWebResponse = (HttpWebResponse)request.GetResponse();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9297408

复制
相关文章

相似问题

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