WebRequest超时 [英] WebRequest Times out

查看:46
本文介绍了WebRequest超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可读取正常运行的网页.第二次WebRequest超时时,两次调用相同的URL除外.
例如第一次使用http://www.yahoo.com/main.html可以正常工作
第二次http://www.yahoo.com/test.html它将超时.
有人知道如何解决这个问题吗?谢谢

I have following code to read a webpage which works fine. Except whenever same url is being called twice second time WebRequest times out.
for example first time http:www.yahoo.com/main.html will work fine
second time http://www.yahoo.com/test.html it will time out.
Anyone knows how to fix this? thanks

public static string ReadWebPage(string url)
        {
            string webPage;
            WebRequest request = WebRequest.Create(url);
            using (Stream stream = request.GetResponse().GetResponseStream())
            {
                StreamReader sr = new StreamReader(stream);
                webPage = sr.ReadToEnd();
                sr.Close();
            }
            return webPage;
        }

推荐答案

http://www.yahoo. com/test.html [ ^ ]

直接在浏览器中打开此页面时找不到该页面.它在yahoo的其他页面上将您重定向.因此,它无法处理两个请求.(一个页面导致另一页面加载)

祝您编程愉快!
:)
http://www.yahoo.com/test.html[^]

this page is not found when open it directly in browser., it is redirecting you on something different yahoo''s page. so, it can''t process two requests.(one page is causing another page to load)

Happy coding!
:)


使用垃圾回收处理导致问题的WebRequest对象.
在方法调用的末尾添加GC.Collect().

谢谢和问候,
拉胡尔·奇特(Rahul Chitte)
Use garbage collection to dispose your WebRequest object which is causing issue.
Add GC.Collect() at end of your method call.

Thanks and regards,
Rahul Chitte


这篇关于WebRequest超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆