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

推荐订阅源

N
News and Events Feed by Topic
D
Docker
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
F
Full Disclosure
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
L
LangChain Blog
H
Help Net Security
B
Blog
T
Tailwind CSS Blog
V
V2EX
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
美团技术团队
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
I
InfoQ
Project Zero
Project Zero
I
Intezer
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Securelist
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
The Hacker News
The Hacker News

博客园 - 匡匡

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 错乱。