2

如果我使用以下代码:

using System;
using System.Net;

namespace ConsoleApplication1
{
    public class TimeoutWebClient : WebClient
    {
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest webRequest = base.GetWebRequest(address);
            webRequest.Timeout = 600000;
            return webRequest;
        }
    }

    class Program
    {
        static void Main()
        {
            WebClient webClient = new TimeoutWebClient();
            webClient.Headers.Add("Referer", @"http://www.finam.ru/analysis/export/default.asp");
            string csv =
                webClient.DownloadString(
                    "http://195.128.78.52/RIZ1.csv?d=d&market=17&em=75118&p=1&df=23&mf=8&yf=2011&dt=23&mt=8&yt=2011&f=RIZ1&e=.csv&datf=11&cn=RIZ1&dtf=1&tmf=1&MSOR=0&sep=3&sep2=1&at=1");
            Console.WriteLine(csv);
        }
    }
}

然后如果服务器很长时间没有应答,我会得到一个异常

System.Net.WebException was unhandled
  HResult=-2146233079
  Message=The underlying connection was closed: An unexpected error occurred on a receive.
  Source=System
  StackTrace:
       at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       at System.Net.WebClient.DownloadString(Uri address)
       at System.Net.WebClient.DownloadString(String address)
       at ConsoleApplication1.Program.Main() in c:\bot\test\webrequest_timeout\ConsoleApplication1\ConsoleApplication1\Program.cs:line 22
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.IOException
       HResult=-2146232800
       Message=Unable to read data from the transport connection: Удаленный хост принудительно разорвал существующее подключение.
       Source=System
       StackTrace:
            at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
       InnerException: System.Net.Sockets.SocketException
            HResult=-2147467259
            Message=Удаленный хост принудительно разорвал существующее подключение
            Source=System
            ErrorCode=10054
            NativeErrorCode=10054
            StackTrace:
                 at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
                 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            InnerException: 
4

2 回答 2

0

我的理解是这不是由于超时

错误代码表示 远程主机强行关闭了现有连接

http://ipswitchft.force.com/kb/articles/FAQ/Why-do-clients-receive-Error-10054-writing-to-web-client-1307565967668

http://support.microsoft.com/kb/2668751

于 2012-12-20T06:07:00.797 回答
0

看起来它在阅读响应时超时?然后,您还想通过使用派生类 HttpWebRequest 的 ReadWriteTimeout 属性来设置读/写超时。

请参阅http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.readwritetimeout.aspx

于 2012-12-20T05:44:33.350 回答