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("http://website.com/");
            // Response VAR
            var response = (HttpWebResponse)request.GetResponse();
            // Cookie Var
            var cookie = new CookieContainer();

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

            request = (HttpWebRequest)WebRequest.Create("http://website.com/main/login");

            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 = "website.com";
            request.Headers.Add("Origin", "http://website.com");
            request.Referer = "http://website.com/";
            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();
            request.CookieContainer.Add(cookie.GetCookies(request.RequestUri));

            // 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);
                stream.Close();
                stream.Dispose();
            }

            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();
                    sReader.Close();
                    sReader.Dispose();
                }

                string strSSIDurl = response.ResponseUri.ToString();

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

            request.Abort();
            response.Close();
            response.Dispose();

            return strSSID;
        }

Answers


you are not disposing the response from the first request.

var request = (HttpWebRequest)WebRequest.Create("http://website.com/");
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...