首页 > Windows开发 > 详细

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

时间:2019-06-11 16:45:48      阅读:190      评论: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请求

原文:https://www.cnblogs.com/cuihongyu3503319/p/11004289.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!