我有一个服务器与多个IP地址.现在我需要与几个服务器通信http协议.每个服务器只接受来自我服务器的指定ip地址的请求.但是当在.NET中使用WebRequest(或HttpWebRequest)时,请求对象将自动选择一个IP地址.我找不到要用地址绑定请求.
有没有这样做?或者我必须自己实现一个webrequest类?
解决方法
您需要使用ServicePoint.BindIPEndPointDelegate回调.
http://blogs.msdn.com/b/malarch/archive/2005/09/13/466664.aspx
The delegate is called before the socket associated with the httpwebrequest attempts to connect to the remote end.
public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint,IPEndPoint remoteEndPoint,int retryCount) { Console.WriteLine("BindIPEndpoint called"); return new IPEndPoint(IPAddress.Any,5000); } public static void Main() { HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://MyServer"); request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); }
原文地址:https://www.jb51.cc/csharp/94233.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。