我是否可以将指定的IP地址用的WebRequest .NET框架? [英] Can I send webrequest from specified ip address with .NET Framework?

查看:444
本文介绍了我是否可以将指定的IP地址用的WebRequest .NET框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个IP地址的服务器。现在,我需要多个服务器以http协议进行通信。每个服务器只接受来自我的服务器的指定IP地址的请求。但是,在.NET中使用的WebRequest(或HttpWebRequest的)时,请求对象会自动选择一个IP地址。我找不到反正绑定一个地址的请求。



反正有这样做?或者我要实现一个WebRequest类自己?


解决方案

您需要使用 ServicePoint.BindIPEndPointDelegate 回调。



http://blogs.msdn.com/b/malarch/archive/2005/09/13/466664.aspx




在与HttpWebRequest的尝试相关的插座连接到远端的委托被调用。




 公共静态IPEndPoint BindIPEndPointCallback(的ServicePoint服务点,IPEndPoint remoteEndPoint,INT RetryCount重)
{
Console.WriteLine(BindIPEndpoint称为);
返回新IPEndPoint(IPAddress.Any,5000);

}

公共静态无效的主要()
{

HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(HTTP:/ /要播);

request.ServicePoint.BindIPEndPointDelegate =新BindIPEndPoint(BindIPEndPointCallback);

HttpWebResponse响应=(HttpWebResponse)request.GetResponse();

}


I have a server with multi ip addresses. Now I need to communicate with several servers with http protocol. Each server only accept the request from a specified ip address of my server. But when using WebRequest(or HttpWebRequest) in .NET , the request object will choose a ip address automatically. I can't find anyway to bind the request with a address.

Is there anyway to do so ? Or I have to implement a webrequest class myself ?

解决方案

You need to use the ServicePoint.BindIPEndPointDelegate callback.

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

}

这篇关于我是否可以将指定的IP地址用的WebRequest .NET框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆