[英]How to Resolve “The operation has timed out” error
我有一个问题,即在下载数据时显示错误“操作已超时”。
我能做些什么来解决这个错误? 我正在使用 Win forms(C#) 这是我的代码,请检查并提出建议。 我应该在哪里更改代码,请帮助我...
public void ProcessData()
{
try
{
string MessageTitle = "";
int pages = Convert.ToInt32(txtPages.Text);
for (int k = Count; k <= pages; k++)
{
string url = "http://www.yellowpages.com/" +StateName.ToLower()+ "/" + CategoryName + "?g=" + StateName + "&page=" + k + "&q=" + CategoryName + "";//txtYP.Text + k;
System.Net.HttpWebRequest httpRequest;
System.Net.HttpWebResponse httpResponse;
System.IO.StreamReader SReader;
string html;
httpRequest = (System.Net.HttpWebRequest)(System.Net.HttpWebRequest.Create(url));
httpRequest.Method = "GET";
httpResponse = (System.Net.HttpWebResponse)(httpRequest.GetResponse());
SReader = new StreamReader(httpResponse.GetResponseStream());
html = SReader.ReadToEnd();
string strDummy = html;
httpResponse.Close();
在请求超时之前多长时间? 您是否能够从 web 浏览器导航到 url?
将HttpWebRequest.ReadWriteTimeout
上的 HttpWebRequest.ReadWriteTimeout 属性设置为比当前值高得多的值。 默认值为 5 分钟。 不知道为什么需要超过 5 分钟。
除了阻塞 getresponse,您还可以使用异步回调( BeginGetResponse
/ EndGetResponse
)。
编辑
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.Net">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.HttpListener">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Sockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Cache">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add
name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="trace.log"
traceOutputOptions = "ProcessId, DateTime"
/>
</sharedListeners>
<switches>
<add name="System.Net"
value="Verbose" />
<add name="System.Net.Sockets"
value="Verbose" />
<add name="System.Net.Cache"
value="Verbose" />
<add name="System.Net.HttpListener"
value="Verbose" />
</switches>
</system.diagnostics>
在应用程序的 app.config 中的configuration
部分中添加此部分。 添加上述内容后,重建解决方案并运行它。 查看应用程序的 bin 目录中写入的 trace.log 以了解更多详细信息。
将此添加到您的代码中:
httpRequest.Timeout = 3600000;
这会将请求超时增加到一小时。
在服务器上执行我的控制台应用程序时,我遇到了同样的问题。 以下解决方案对我有用:
在 Internet 选项中取消选中 LAN 设置下的自动配置脚本并检查自动检测设置。 它解决了我的操作超时错误问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.