Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
硬代码IPEndpoint在ASP.Net中使用ServicePoint.BindIPEndPointDelegate时出现异常_Asp.net_Delegates_Httpwebrequest - Fatal编程技术网

硬代码IPEndpoint在ASP.Net中使用ServicePoint.BindIPEndPointDelegate时出现异常

硬代码IPEndpoint在ASP.Net中使用ServicePoint.BindIPEndPointDelegate时出现异常,asp.net,delegates,httpwebrequest,Asp.net,Delegates,Httpwebrequest,我正在尝试为我的webRequest绑定出站IP HttpWebRequest reqhttp = (HttpWebRequest)req; reqhttp.ServicePoint.BindIPEndPointDelegate = new System.Net.BindIPEndPoint(BindIPEndPointCallback); reqhttp.Credentials = null; reqhttp.AuthenticationLevel = AuthenticationLevel.

我正在尝试为我的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();
远程主机强制关闭了现有连接

我不明白这里怎么了


任何人都可以帮助理解此代码中的错误并解决问题。

尝试类似的方法

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();

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

感谢您的帮助。我的处理方式不同。我稍后会检查你的建议。