locked
HttpWebRequest.GetRequestStream throws Timeout WebException RRS feed

  • Question

  • String output = "username=" + username + "&password=" + password;
    Byte[] outputBuffer = System.Text.Encoding.Default.GetBytes(output);

    System.Net.HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

    req.ContentType = "application/x-www-form-urlencoded";
    req.Method = "POST";

    req.Timeout = 30000;

    req.ContentLength = outputBuffer.Length;
    req.ContentType = "application/x-www-form-urlencoded";
    Stream reqstream = req.GetRequestStream();
    try
    {
     reqstream.Write( outputBuffer, 0, outputBuffer.Length );
    }
    finally
    {
     reqstream.Close();
    }

    When url points to a website on the local host this runs fine.
    However if url points to a website on another host on the local network, req.GetRequestStream() often throws a WebException with Status property == Timeout.
    It will sometimes work but more often than not it throws a timeout.

    If I try to access the same site through IE it runs fine.

    This is .NET 1.1 on Windows 2003 SP1

    Wednesday, February 28, 2007 3:08 PM

Answers

All replies

  • I found your post looking for a solution to very similar problem...

    for me it was not sporadic though, I simply could not get in a 2nd POST or GET in the same session even though I did use a 2nd call to the Create(url) method, creating a new req object.

    well it turns out I failed to close the Response object.

    Though you don't have one here, and though above seems like it always executes only 1 request in same session, perhaps for lack of any other responses here you could add this code and try again.

    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    res.Close();

    after that, I'd be sniffing this out w/netmon or ethereal.

    Wednesday, March 7, 2007 11:47 PM
  • That is correct: your request is not issued until you call GetResponse() and you cannot issue more than 1 post in 1 request. And you have to always call response.Close()

    Here is a sample for this
    http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx

    Mariya

    Thursday, March 8, 2007 12:33 AM
    Moderator
  • i faced same problem, try to resolve it and found the solution:

    add following code to web.config:

    <

    system.net><defaultProxy>

    <proxy usesystemdefault="False" proxyaddress=http://proxyAddress:port bypassonlocal="True" autoDetect="False" />

    </

    defaultProxy></system.net>

    Thursday, May 27, 2010 12:29 PM