zoukankan      html  css  js  c++  java
  • 解决WebClient或HttpWebRequest首次连接缓慢问题

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

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

    【问题描述】

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

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

    【解决方案】

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

    <?xml version="1.0"?>  
    <configuration>  
      <startup>  
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>  
      </startup>  
      <system.net>  
        <defaultProxy  
            enabled="false"  
            useDefaultCredentials="false" >  
          <proxy/>  
          <bypasslist/>  
          <module/>  
        </defaultProxy>  
      </system.net>  
    </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.

    【总结】

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

    希望能帮到您!

    转自:http://blog.csdn.net/rrrfff/article/details/6170653?reload

    慎于行,敏于思!GGGGGG
  • 相关阅读:
    不可重叠最长重复子串
    离散化
    hash是一门优雅的暴力
    Detect the Virus (字符串转化+AC自动机)
    病毒侵袭(AC自动机变形)
    hdu2069(Coin Change)
    Hie with the Pie(poj3311)
    poj3254(状压dp入门第一道题,很详细)
    map系统学习
    ACM-ICPC 2018 南京赛区网络预赛 B. The writing on the wall
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/5569772.html
Copyright © 2011-2022 走看看