Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebRequest just hanging on one computer

I've got a piece of code the goes out and does an HttpWebRequest to Google Wallet. The code works just fine on all my machines, except for this one computer at work that I'm on right now. The Internet on this computer works just fine (as I'm using it right now to type this question) and we have no proxy server at work.

The problem is that it just hangs. It doesn't even timeout. It just hangs with no error message or anything and I have to force close it. Here is the code:

    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);
            using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
            {
                string html = sr.ReadToEnd();
                string galxValue = ParseOutValue(html, "GALX");

                return GetLoginHtml2(galxValue, cookieJar);
            }
        }
    }

Stepping through the code on the Visual Studio debugger, I know that it hangs when it hits the following line:

using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))

Specifically, the request.GetResponse() part is what's hanging. Running Fiddler, all I see is a gray entry that looks like this:

 3     200    HTTP            Tunnel to     accounts.google.com:443            0

There is nothing else. No response body. Do anyone have any suggestions on what could be going on? The fact that its happening on just this one computer tells me that it may not be a programming issue.

like image 381
Icemanind Avatar asked Dec 08 '14 16:12

Icemanind


1 Answers

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:

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);
    }
    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. Silly .NET garbage collector

like image 100
Icemanind Avatar answered Oct 15 '22 21:10

Icemanind