views:

973

answers:

3

This is not the first time I'm using this method to send a POST request, and I never had any problems:

    public static Stream SendPostRequest(Uri uri, byte[] postData)
    {
        var request = WebRequest.Create(uri);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postData.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(postData, 0, postData.Length);
        requestStream.Close();
        return request.GetResponse().GetResponseStream();
    }

On request.GetRequestStream() I'm getting a System.Net.WebException: The underlying connection was closed: An unexpected error occured on a send.
Even more interesting, it works perfectly well on some machines, but doesn't work on my machine (Windows 7 Beta) and production server (Windows Server 2008). More information:

Works - Windows Xp - .NET 2.0
Works - Windows Xp - .NET 3.5
Works - Windows Server 2003 - .NET 3.0
Doesn't work - Windows Vista - .NET 3.5
Doesn't work - Windows Server 2008 - .NET 3.5
Doesn't work - Windows 7 Beta - .NET 3.5 SP1

Tried:

  • Bunch of stuff from here, nothing helped.
  • Using WebClient, nothing changed.
  • Tweaking these options, but didn't notice any notable difference.
  • Tried WireShark. A very good tool.

[Solved. Kinda]
I forgot to mention, but Uri was https... I tried http and it worked. Can't believe, I didn't try that sooner...
Still, I would appreciate, if someone would shine a light on this whole situation.

+1  A: 

My first plan of attack would be to use WireShark to see what's happening at the network level in each case. See what each machine is sending.

Also, you've noted differences between operating systems, but do they all have the exact same version of .NET (down to SP) installed?

Jon Skeet
None of the entries solved it, but this was the most useful, that's why it was accepted. I'll accept a different answer, if there'll be a better answer.
Mindaugas Mozūras
A: 

Do GETs work? Perhaps it is a proxy config issue (proxycfg etc).

Also - to simplify things (reduce the number of unknowns), consider using WebClient to do the post:

using (WebClient client = new WebClient())
{
    client.Headers.Add("content-type","application/x-www-form-urlencoded");
    client.UploadData(address, data);
    // or more simply
    client.UploadValues(address, nameValuePairs);
}
Marc Gravell
A: 

Try tweaking one or more of the following options:

  • SendChucked
  • AllowAutoRedirect
  • TransferEncoding

Also try to note differences between them with your various configurations.

leppie