简体   繁体   中英

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. I then ported it to Mono, because I needed the application to run in a Linux console.

The application is working fine in Mono, but now it stopped working in Windows. The source code is identical. It is literally a copy and paste of the Mono source code. When I run the application in Windows, it just comes up with a black console window and "hangs". 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. 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. 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. Anyway, its working now, in both Windows and Mono! Silly .NET garbage collector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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