C# 使用WebClient時,在多網卡IP時,指定IP發送Web請求


需要定義一個類,重寫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即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM