HttpWebRequest just hangs on one computer
I have a piece of code that goes out and executes HttpWebRequest
in Google Wallet. The code works very well on all my machines, except for this computer at work, which I am currently finding. The internet on this computer works really well (as I'm using it right now to type this question) and we don't have a proxy at work.
The problem is, it just hangs. It's not even waiting time. It just hangs with no error message or whatever and I have to force it to 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);
}
}
}
While executing the code in the Visual Studio debugger, I know it freezes when it hits the following line:
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
In particular, the part request.GetResponse()
is what hangs. Running Fiddler, all I can 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. Does anyone have any suggestions on what might be going on? The fact that this only happens on one computer tells me it might not be a programming problem.
source to share
I finally figured out the problem with this. I am posting this answer in the hopes that it might help someone else with some kind of imminent headache in the future.
Here is my working 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);
}
}
My guess is that my requestStream was not collected and garbage-collected before receiving a stream of responses. I think the response stream was open and read before the request stream finished writing its bytes. .NET Cartoon Garbage Collector
source to share