码迷,mamicode.com
首页 > Windows程序 > 详细

C# 使用WebClient时,在多网卡时,指定IP发送Web请求

时间:2015-01-12 16:13:55      阅读:540      评论:0      收藏:0      [点我收藏+]

标签:

需要定义一个类,重写GetWebRequest,在方法内,指定IP地址

 

    public class MyWebClient : WebClient
    {
        private IPAddress ipAddress;

        public MyWebClient(IPAddress ipAddress)
        {
            this.ipAddress = ipAddress;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = (WebRequest)base.GetWebRequest(address);

            ((HttpWebRequest)request).ServicePoint.BindIPEndPointDelegate += (servicePoint, remoteEndPoint, retryCount) =>
            {
                return new IPEndPoint(ipAddress, 0);
            };

            return request;
        }
    }

 

使用时,  在实例化构造函数中传入所需IP即可

C# 使用WebClient时,在多网卡时,指定IP发送Web请求

标签:

原文地址:http://www.cnblogs.com/xyz0835/p/4218475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!