惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
Jina AI
Jina AI
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
小众软件
小众软件
M
MIT News - Artificial intelligence
MyScale Blog
MyScale Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
月光博客
月光博客
L
LangChain Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
C
Check Point Blog
U
Unit 42
人人都是产品经理
人人都是产品经理

博客园 - 匡匡

Vue 父子组件通信方式 Vue: 组件扩展 IIS8 下 JS, CSS 等静态文件出现 500 错误 使用 ffmpeg 转换 mov 视频 使用 ildasm 和 ilasm 修改程序集的的引用信息 2020-01-08 工作日记:无题 .Net Core 程序集管理说明(加载) .NET CORE 动态加载 DLL 的问题 ASP.NET 后台 COOKIE 的设置 使用 sql server 默认跟踪分析执行的 SQL 语句 Nginx深入详解之upstream分配方式 使用 HttpWebRequest 类做 POST 请求没有应反 webpack 里的 import, exports 实现原理 使用 pdf.js 查看发票时,显示不了台头和印章的解决办法 Flex 布局里 input 宽度最小 150px 的问题, 浏览器 BUG? 使用像素单位设置 EXCEL 列宽或行高 sweetalert 快速显示两个提示, 第二个显示不出的问题 加权轮询和加权随机算法 在 Docker 中部署 ASP.NET CORE 应用
WebClient 指定出口 IP
匡匡 · 2020-05-09 · via 博客园 - 匡匡

当一个服务器有多个 IP 时, 在使用 webclient 对象需要指定出口 IP

public class MyWebClient : WebClient
{
    private IPAddress ipAddress;

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

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

        // 把 KeepAlive 设置为 false 表示连接用完就关
        request.KeepAlive = false;
        request.ServicePoint.ConnectionLeaseTimeout = 0;
        request.ServicePoint.BindIPEndPointDelegate += (servicePoint, remoteEndPoint, retryCount) =>
        {
            return new IPEndPoint(ipAddress, 0);
        };

        return request;
    }
}

KeepAlive 和 ConnectionLeaseTimeout 这两个属性非常重要, 不然 ServicePoint 会有缓存, 导致出口 IP 错乱。