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

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 南桥一梦

Windows Hyper-v 开启嵌套虚拟化的方法 MySQL命令记录 Windows Server 2019 安装Windows Docker和.NET Framework 4.8镜像 Windows Docker 固定容器IP地址 透明网络驱动程序 MySQL优化 右键菜单怎样添加“在此处打开命令提示符”选项 MemSQL与MySQL不兼容问题总结 Ubuntu Server 18.04 修改网路配置 How to Install MemSQL Metro UI 菜单(Winform) Windows Server 2008 系统设置集合 Ubuntu 12.04和MySQL5.5安装 MySQL 参数设置 VS中如何自定义新建文件模板(添加自定义版权信息) Microsoft Windows Server 2008 R2 CHS 官方最新正版下载 在Hyper-V中安装和配置Ubuntu Server 11.04 10.10 Ubuntu MySQL Master-Master InnoDb Utf-8 配置文件 Ubuntu MySQL热备份安装 HOWTO:为 Hyper-V 配置外部网络
解决WebClient或HttpWebRequest首次连接缓慢问题
南桥一梦 · 2011-11-13 · via 博客园 - 南桥一梦

【编程环境】Visual Studio 2010, NET4.0

【开发语言】C#, 理论上VB.NET等依赖.NET Framework框架的语言均受此影响

【问题描述】

使用HttpWebRequest抓取网页内容,但首次请求总是莫名奇妙的阻塞在Request.GetResponse();上,不过一旦这次请求成功,后续的操作就很快了(如果是针对同一对象)。

相同的代码编译在NET3.5环境中却一切正常,而在NET4.0环境中执行就出这问题,难道是一个BUG?

【解决方案】

在配置文件中(.config)中添加配置节:

  1. <?xml version="1.0"?>
  2. <configuration>
  3. <startup>
  4. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  5. </startup>
  6. <system.net>
  7. <defaultProxy
  8. enabled="false"
  9. useDefaultCredentials="false" >
  10. <proxy/>
  11. <bypasslist/>
  12. <module/>
  13. </defaultProxy>
  14. </system.net>
  15. </configuration>

【问题所在】 

.NET4.0中的默认代理是开启的,而我并没有设置!故只有等待超时后才会绕过代理,这就阻塞了.

【参考资料】

"It's not set at all in app.cong or machine.config. Hmm. If I'm reading the
MSDN docs right, the default for defaultProxy.enabled is TRUE if the element
isn't specified at all. That would be consistent with my observations.
"

http://msdn2.microsoft.com/en-us/library/kd3cf2ex(VS.80).aspx

【问题引申】

如果在其它版本的.NET环境中遇到类似问题,不妨尝试WebClient.Proxy = null;或HttpWebRequest.Proxy = null.

【总结】

问题往往出于不注意,我们要多加'小心'.

希望能帮到您!