C#Webclient访问几个网站时超时

时间:2015-04-16 05:15:17

标签: c# visual-studio-2010 webclient connection-timeout

我可以在Windows 7中使用webclient访问所有网页但是, 相同的代码无法在Windows Server 2008 R2上运行。

以下是其中一个示例网站:https://www.euronext.com/在win7中使用webclient但在2008 R2服务器上无法正常工作。

以下是我使用的代码:

using (WebClient client = new WebClient())
{
    client.Headers["User-Agent"] =
    "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
    "(compatible; MSIE 6.0; Windows NT 5.1; " +
    ".NET CLR 1.1.4322; .NET CLR 2.0.50727)";

    // Download data.
    byte[] arr = client.DownloadData("https://www.euronext.com/");

    // Write values.
    Console.WriteLine("--- WebClient result ---");
    Console.WriteLine(arr.Length);
}

我也试过以下代码:

WebClient client = new WebClient();

// Set user agent and also accept-encoding headers.
client.Headers["User-Agent"] =
    "Googlebot/2.1 (+http://www.googlebot.com/bot.html)";
client.Headers["Accept-Encoding"] = "gzip";

// Download data.
byte[] arr = client.DownloadData("https://www.euronext.com/");

// Get response header.
string contentEncoding = client.ResponseHeaders["Content-Encoding"];

// Write values.
Console.WriteLine("--- WebClient result ---");
Console.WriteLine(arr.Length);
Console.WriteLine(contentEncoding);
如果我在这里搞什么话,有人可以指导我吗

感谢您的期待。

此致 Sharez

异常详情

System.Net.WebException:底层连接已关闭:发送时发生意外错误。 ---> System.IO.IOException:从传输流中收到意外的EOF或0字节。 System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)上的System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)---内部异常堆栈跟踪结束---系统的System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)位于C:\ myProject \ MsHtml \ Classes \ WebClient \ CookieAwareWebClient.cs中的CapitalIQ.WebCrawler.Lib.CookieAwareWebClient.GetWebResponse(WebRequest请求,IAsyncResult结果)中的.Net.WebClient.GetWebResponse(WebRequest请求,IAsyncResult结果):系统中的第62行.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result)

0 个答案:

没有答案