简体   繁体   English

HttpWebRequest导致应用程序挂起

[英]HttpWebRequest causes application to hang

I'm having a weird issue. 我有一个奇怪的问题。 I created an application in Visual Studio 2013 and the application worked fine in Windows. 我在Visual Studio 2013中创建了一个应用程序,该应用程序在Windows中运行良好。 I then ported it to Mono, because I needed the application to run in a Linux console. 然后,我将其移植到Mono,因为我需要该应用程序在Linux控制台中运行。

The application is working fine in Mono, but now it stopped working in Windows. 该应用程序在Mono中运行正常,但是现在停止在Windows中运行。 The source code is identical. 源代码是相同的。 It is literally a copy and paste of the Mono source code. 它实际上是Mono源代码的复制和粘贴。 When I run the application in Windows, it just comes up with a black console window and "hangs". 当我在Windows中运行该应用程序时,它只是带有一个黑色的控制台窗口并“挂起”。 Here is the code thats hanging: 这是悬挂的代码:

static void Main(string[] args)
{
    string orders = GetLoginHtml();

    Console.WriteLine(orders);
}

private static string GetLoginHtml()
{
    var request = (HttpWebRequest)WebRequest.Create(LoginUrl);
    var cookieJar = new CookieContainer();

    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.CookieContainer = cookieJar;
    using (var requestStream = request.GetRequestStream())
    {
        string content = "Email=" + Username + "&Passwd=" + Password;
        requestStream.Write(Encoding.UTF8.GetBytes(content), 0, Encoding.UTF8.GetBytes(content).Length);

        // The next line is where it hangs

        using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
        {
            string html = sr.ReadToEnd();
            string galxValue = ParseOutValue(html, "GALX");

            return GetLoginHtml2(galxValue, cookieJar);
        }
    }
}

I commented where the line is hanging. 我评论了线路的悬挂位置。 I'm at a loss as to why, at the very least, its not giving me a time out error. 我茫然为什么至少没有给我超时错误。 I ran fiddler and I see it trying to go out, but Fiddler is just reporting a closed connection immediately. 我运行了提琴手,发现它试图退出,但是提琴手只是立即报告已关闭连接。 My Internet is working just fine and the URL works fine, if I go to it using a browser. 如果使用浏览器访问,我的Internet正常运行,URL正常运行。 Any suggestions? 有什么建议么?

I finally figured out the issue with this. 我终于弄明白了这个问题。 I'm posting this answer in hopes it might help someone else with some inevitable head scratching in the future. 我正在发布此答案,希望它可以在将来对某些不可避免的头部刮伤有所帮助。

Here is my working code now: 现在是我的工作代码:

using (var requestStream = request.GetRequestStream())
{
    string content = "Email=" + Username + "&Passwd=" + Password;
    requestStream.Write(Encoding.UTF8.GetBytes(content), 0, Encoding.UTF8.GetBytes(content).Length);
}
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
    string html = sr.ReadToEnd();
    string galxValue = ParseOutValue(html, "GALX");

    return GetLoginHtml2(galxValue, cookieJar);
}

My guess is that my requestStream wasn't being garbage collected and closed before getting the response stream. 我的猜测是在获取响应流之前,没有对我的requestStream进行垃圾收集和关闭。 I think the response stream was being open and read before the request stream finished writing its bytes. 我认为响应流在请求流完成其字节写入之前就处于打开和读取状态。 Mono must have been smart enough to finish the writing before starting the reading. Mono必须足够聪明才能在开始阅读之前完成写作。 Anyway, its working now, in both Windows and Mono! 无论如何,它现在可以在Windows和Mono中运行! Silly .NET garbage collector. 愚蠢的.NET垃圾收集器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM