3

The following code hangs when I try to run it on .NET 3.5 (it works when I set the project to .NET 4)

(I've waited like 2 minutes - It seems to freeze)

        HttpWebRequest l_oRequest;
        HttpWebResponse l_oResponse;

        if (m_bLoggedIn)
            return true;

        m_oSession = new SSessionIdentifier();
        m_oSession.Cookies = new CookieContainer();

        string l_sHost = "https://website.com";

        // Start the login sequence
        l_oRequest = GetNewRequest(l_sHost, "Login.aspx", m_oSession.Cookies);
        l_oRequest.Timeout = 5000;
        l_oRequest.ReadWriteTimeout = 5000;
        l_oRequest.Method = "POST";
        l_oRequest.Referer = "https://website.com";

        // Set form parameters
        string l_sFormParameters = "user=" + m_sUsername;

        // Convert them to the HTTP stream
        byte[] l_oRequestBuffer = UTF8Encoding.ASCII.GetBytes(l_sFormParameters);
        var l_oRequestStream = l_oRequest.GetRequestStream();
        l_oRequestStream.Write(l_oRequestBuffer, 0, l_oRequestBuffer.Length);

        // I'm setting this to false because otherwise i can't get the cookies i want. And I love them cookies!
        l_oRequest.AllowAutoRedirect = false;

        // Now start the redirection sequence until we get to what we want
        int l_iDTPos = -1;
        // This line hangs
        l_oResponse = (HttpWebResponse)l_oRequest.GetResponse();
        while (l_oResponse.StatusCode == HttpStatusCode.Found)
        {
                // Yada Yada

This is the GetNewRequest function:

    private HttpWebRequest GetNewRequest(string host, string targetUrl, CookieContainer a_oCookieJar)
    {
        HttpWebRequest l_oRequest = (HttpWebRequest)HttpWebRequest.Create(host + targetUrl);
        l_oRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2";
        l_oRequest.ContentType = "application/x-www-form-urlencoded";
        l_oRequest.AllowAutoRedirect = false;
        l_oRequest.CookieContainer = a_oCookieJar;

        return l_oRequest;
    }

I couldn't find any change between 3.5 and 4 in the documentation. Has anyone ran into such a problem?

1
  • Your l_sHost is malformed - "*https:/*website.com" should be "*https://*website.com". Not sure if that's in your code, or just a typo in your question. Oct 29, 2012 at 12:50

2 Answers 2

4

I was hitting this same issue where I was calling a RESTful service using HttpWebRequest and my code worked great in .Net 4+ and hung on the GetResponse() line in .Net 3.5, 3.0. 2.0. I ended up trying the sample code from this article:

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

and got it working in all versions of .Net. I'm guessing I wasn't setting something correct or closing a resource, but for anyone else who hits this give the article a try.

1
  • The thing you probably did different is close the request stream. I have just had the same problem and used your link to that example to see that. .net 4 must close the stream automatically when you call Write()
    – Ronnie
    Jan 28, 2015 at 22:09
0

try using code given in following link, it used Stream class to get repose data for the httpwebrequest made :

http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx

http://forums.asp.net/t/1178426.aspx/1

1
  • Thanks for your answer. The link talks about HttpWebResponse.GetResponseStream, but my application hangs on getting the response using GetResponse - Which is needed to use GetResponseStream
    – Nitay
    Oct 29, 2012 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.