Application hangs on GetRequestStream() after first request

I've googled and searched here. Some suggest that streams were not being close, others suggested that it's a connection limit with ServicePointManager.DefaultConnectionLimit being set to 1. However, none of these seem to work.

My problem is, when i use this for the first time, it works:

using (var stream = request.GetRequestStream())
     var data = Encoding.UTF8.GetBytes(post.ToString());
     stream.Write(data, 0, data.Length);

When I use it a second time, it freezes. Yes, I'm disposing my stream, yes im Aborting and closing my response and requests.

Here is my entire code segment:

public string get_hash(string strUsername, string strPassword, string strUniverse)
            // Request VAR
            var request = (HttpWebRequest)WebRequest.Create("");
            // Response VAR
            var response = (HttpWebResponse)request.GetResponse();
            // Cookie Var
            var cookie = new CookieContainer();

            ServicePointManager.DefaultConnectionLimit = 100;            
            request.Timeout = 10;

            request = (HttpWebRequest)WebRequest.Create("");

            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
            request.Headers.Add("Accept-Language", "en-US,en,q=0.8");
            request.Headers.Add("Cache-Control", "max-age=0");
            request.ContentType = "application/x-www-form-urlencoded";
            request.Host = "";
            request.Headers.Add("Origin", "");
            request.Referer = "";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36";
            request.Method = "POST";

            request.CookieContainer = new CookieContainer();

            // SET POST DATA HERE
            var post = HttpUtility.ParseQueryString(string.Empty);
            post.Add("uni", strUni);
            post.Add("login", strUsername);
            post.Add("pass", strPassword);

            using (var stream = request.GetRequestStream())
                var data = Encoding.UTF8.GetBytes(post.ToString());
                stream.Write(data, 0, data.Length);

            response = (HttpWebResponse)request.GetResponse();

            string strSSID = "Failed";

            if (response.StatusCode == HttpStatusCode.OK)
                var data = string.Empty;
                using (var sReader = new StreamReader(response.GetResponseStream()))
                    data = sReader.ReadToEnd();

                string strSSIDurl = response.ResponseUri.ToString();

                int intSSIDurlStart = strSSIDurl.IndexOf("PHPSESSID=") + 10;
                strSSID = strSSIDurl.Substring(intSSIDurlStart);


            return strSSID;


you are not disposing the response from the first request.

var request = (HttpWebRequest)WebRequest.Create("");
var response = (HttpWebResponse)request.GetResponse(); // Not disposed

We had similar problem and disposing the response properly helped us.

Need Your Help

Django GET or POST data will NOT bind to form

django django-forms

My form data will NOT bind no matter what I do. As you can see in my code, I have set up dynamic 'mutlipleChoice' fields (aka drop down menus). I won't go into why but the must be dynamically gen...