Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request.GetResponse gives always a Timeout

I made a Function for a program, which does work when the Request Type is GET, if it is POST, it always produces a Timeout Exception(and the timeout of 50s wasnt reached) on the Line HttpWebResponse response = (HttpWebResponse)request.GetResponse();
I tried many things, but I doesnt found out why, may someone here know it.

Edit: Got it to work, if someone is interested: https://gist.github.com/4347248

Any help will be greatly appreciated.

My Code is:

public ResRequest request(string URL, RequestType typ, CookieCollection cookies, string postdata ="", int timeout= 50000)
    {
        byte[] data;
        Stream req;
        Stream resp;

        HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
        request.Timeout = timeout;
        request.ContinueTimeout = timeout;
        request.ReadWriteTimeout = timeout;
        request.Proxy = new WebProxy("127.0.0.1", 8118);
        request.Headers.Add(HttpRequestHeader.AcceptLanguage, "de");
        request.Headers.Add("UA-CPU", "x86");


        request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618) ";
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(cookies);
        if (typ == RequestType.POST)
        {
            data = System.Text.Encoding.Default.GetBytes(postdata);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            req = request.GetRequestStream();//after a few tries this produced a Timeout error
            req.Write(data, 0, data.Length);
            req.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();//This line produces a Timeout Exception
            resp = response.GetResponseStream();

            if ((response.ContentEncoding.ToLower().Contains("gzip"))) 
            {
                resp = new System.IO.Compression.GZipStream(resp, System.IO.Compression.CompressionMode.Decompress);
            } else if ((response.ContentEncoding.ToLower().Contains("deflate"))) {
                resp = new System.IO.Compression.DeflateStream(resp, System.IO.Compression.CompressionMode.Decompress);
            }

            return new ResRequest() { result = new System.IO.StreamReader(resp, System.Text.Encoding.UTF8).ReadToEnd(), cookies = response.Cookies, cstring = cookiestring(response.Cookies) };

        }
        else
        {
            request.Method = "GET";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            resp = response.GetResponseStream();

            if ((response.ContentEncoding.ToLower().Contains("gzip")))
            {
                resp = new System.IO.Compression.GZipStream(resp, System.IO.Compression.CompressionMode.Decompress);
            }
            else if ((response.ContentEncoding.ToLower().Contains("deflate")))
            {
                resp = new System.IO.Compression.DeflateStream(resp, System.IO.Compression.CompressionMode.Decompress);
            }

            return new ResRequest() { result = new System.IO.StreamReader(resp, System.Text.Encoding.UTF8).ReadToEnd(), cookies = response.Cookies, cstring = cookiestring(response.Cookies) };
        }

    }
like image 402
Tearsdontfalls Avatar asked Dec 19 '12 15:12

Tearsdontfalls


1 Answers

So does it hang on req.GetRequestStream() every time, or does it work "a few tries" and then hang?

If it works a few times and then hangs, it's possible that you're not closing the requests properly, which is causing you to run out of connections. Make sure to Close() and/or Dispose() the HttpWebResponse objects and all of the Streams and Readers that you're creating.

like image 67
Brian Reischl Avatar answered Oct 18 '22 12:10

Brian Reischl