我正在尝试将我的出站IP绑定到我的webRequest
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();
远程主机强行关闭现有连接
我不明白这里有什么问题。
任何人都可以帮助理解此代码中的错误并解决问题。
答案 0 :(得分:1)
GetRequestStream()方法将首先触发BindIPEndPointDelegat,然后它将尝试连接到远程服务器。如果绑定到不存在的本地端点,或者远程服务器不可用,则会出现异常。
答案 1 :(得分:0)
尝试这样的事情
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();