我正试图为我的webRequest绑定我的出站IP
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
}由于某些原因,当我这样做时,它会抛出一个错误。
如果未能执行此行
Stream dataStream = reqhttp.GetRequestStream();现有连接被远程主机强制关闭
我不明白这里出了什么问题。
任何一个人都能帮助理解这段代码的错误并解决这个问题吗?
发布于 2013-09-04 01:11:27
GetRequestStream()方法将首先触发BindIPEndPointDelegat,然后尝试连接到远程服务器。如果绑定到不存在的本地终结点,或者远程服务器不可用,则会得到异常。
发布于 2012-02-15 17:02:03
试试像这样的东西
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();https://stackoverflow.com/questions/9297408
复制相似问题