C#HttpWebRequest突然挂起程序.没早 [英] C# HttpWebRequest hangs program suddenly. Did not earlier

查看:95
本文介绍了C#HttpWebRequest突然挂起程序.没早的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早些时候,我发出了一个运行良好的HttpWebRequest,我的StreamReader完美地读取了网站的HTML.

Earlier I made an HttpWebRequest that worked perfectly fine, and my StreamReader read the HTML of the website perfectly.

但是突然之间,在测试了它的功能并确认它可以正常工作之后,当涉及到StreamReader行时,它会挂起该程序. 我尝试删除此行,然后代码继续.

But all of the sudden, after having tested it's functionality and confirmed that it worked many times, it hangs the program when it comes to the StreamReader line. I have tried removing this line, and the code continued.

问题是;我尝试输入的网站与我需要使用的网站不同(我输入了www.google.com),它工作得很好.因此,我的错误结论是,仅我需要使用的网站就无法访问了,这使我认为终端(该网站)正在取消我的连接或阻止我或其他东西.但! HttpWebRequest本身不会挂起或挂起任何东西,这必须表示它已成功建立了对网站的请求?

The thing is; I tried inputting a different website than the one I need to use, (I put in www.google.com) and it worked perfectly fine. So my error conclusion is, that it is only the website I need to use that I can't access anymore which makes me think that the endpart (the website) is cancelling my connection or blocking me or something. BUT! The HttpWebRequest itself doesn't hang or anything, which must mean that it successfully established a request to the website?

闲聊,这是代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("website here");
MessageBox.Show("1"); //This is shown.
string HTMLLink = (new     StreamReader(request.GetResponse().GetResponseStream())).ReadToEnd(); //This is where the     program hangs....
MessageBox.Show("2"); //This is not shown! Below this isn't being executed.
if (HTMLLink.Length > 0)
{
    HTMLLink = HTMLLink.Substring(HTMLLink.IndexOf("uuu"), HTMLLink.Length -     HTMLLink.IndexOf("uuu"));
    HTMLLink = HTMLLink.Substring(0, HTMLLink.IndexOf("\" TARGET="));
    request = (HttpWebRequest)WebRequest.Create(HTMLLink);
    string HTML = (new     StreamReader(request.GetResponse().GetResponseStream())).ReadToEnd();
    if (HTML.Length > 0 && HTML.Contains(" </script><br><br><br>") && HTML.Contains("    <br><br><script "))
    {
        HTML = HTML.Substring(HTML.IndexOf(" </script><br><br><br>") + 22,     HTML.IndexOf("<br><br><script "));
        HTML = HTML.Substring(0, HTML.IndexOf("<br><br><script "));
        HTML = HTML.Replace("\r\n", "");
        HTML = HTML.Replace("\n", "");
        HTML = HTML.Replace("<br>", "\r\n");
        HTML = HTML.Replace("<BR>", "\r\n");
        HTML = HTML.Replace("<br />", "\r\n");
        HTML = HTML.Replace("<BR />", "\r\n");
        textBox.Text = HTML;
    }
}

并且请记住,它比突然开始挂起的效果更早,并且在www.google.com上也可以正常工作.

And please keep in mind that it worked perfectly earlier then all of the sudden it started hanging, and that it works fine with www.google.com.

顺便说一句,是的,我已经做了很多搜索.没有有用的结果.

And by the way, yes I have done many searches. No useful results.

我已经尝试过超时了,它确实超时了. 也许网站阻止了我的程序以为它是蜘蛛?然后怎样呢?

I have tried the timeout already, it does timeout. Maybe the website has blocked my program thinking it's a spider? what then?

每次我到达StreamReader时(无论如何设置),它都开始挂起. 而且它一直挂着,没有产生任何结果. 这仅发生在lyrics007.com上,这是我需要的确切网站. google可以正常工作.

Everytime when I reach the StreamReader (no matter how I set it up) it starts to hang. And it keeps hanging, it doesn't deliver any result. This ONLY happens with lyrics007.com which is the exact website I need. It works fine with google.

请帮助!

提前谢谢!

推荐答案

WebRequest.GetResponse()是阻止调用.它将等待直到成功连接并接收到响应,然后再将控制权返回给调用者,否则将抛出异常.此行为无法修改.

WebRequest.GetResponse() is a blocking call. It will wait until it can successfully connect and receive the response before it returns control to the caller, or will throw an exception if unsuccessful. This behaviour can't be modified.

尽管您通常不希望您的应用程序等待发生的事情,所以通常将GetResponse()调用委托给另一个线程,这样您就可以在当前线程中继续进行其他工作.

You usually don't want your application to sit waiting for something to happen though, so you usually delegate the GetResponse() call to another thread, so you can continue doing other work in the current thread.

解决此问题的常用方法是异步调用.您将调用BeginGetResponse(),而不是调用GetResponse,传递一个应在操作完成时执行的函数(例如,包含当前方法的其余部分,以及对EndGetResponse()的调用).可以在后台线程等待响应时将执行控制权传递回调用方,该线程由.NET线程池自动为您处理.

The usual way to overcome this problem is to call asynchronously. Rather than a call to GetResponse, you will call BeginGetResponse(), passing in a function which should be executed when the operation completes (eg, containing the remainder of your current method, plus a call to EndGetResponse()). Control of execution can be passed back to the caller whilst the response is being waited for in a background thread, handled for you automatically by the .NET threadpool.

这篇关于C#HttpWebRequest突然挂起程序.没早的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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