Bug 4640 - WebRequest::GetRequestStream blocks indefinitely (up to timeout)
Summary: WebRequest::GetRequestStream blocks indefinitely (up to timeout)
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Gonzalo Paniagua Javier
: 5210 ()
Depends on:
Reported: 2012-04-25 09:40 UTC by Marek Safar
Modified: 2012-05-22 05:43 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Marek Safar 2012-04-25 09:40:30 UTC

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
    public class WebRequestPostExample
        public static void Main ()
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes (postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream ();
            // Write the data to the request stream.
            dataStream.Write (byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close ();
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
Comment 1 Gonzalo Paniagua Javier 2012-04-25 17:03:14 UTC
I get the same result running xsp locally with mono-2-10/02e06a6 (, 2.10.5 from packages and master/bd86454 (2.11.1):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>Welcome to Mono XSP!</title>
Comment 2 Marek Safar 2012-04-30 10:38:48 UTC
Finally I was able to produce correct repro

using System;
using System.IO;
using System.Net;
using System.Text;

namespace WebTester
	class Program
		static void Main (string[] args)
			string port;
			if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
				port = "810";
			} else {
				port = "8810";

			var listener = CreateListener (l => {
				try {
					Console.WriteLine (l.Request.ContentLength64);
					Console.WriteLine ("hit");
				} catch {
			}, port);

			WebRequest request = WebRequest.Create ("http://localhost:" + port);
			request.Method = "POST";
			string postData = "This is a test that posts this string to a Web server.";
			byte[] byteArray = Encoding.UTF8.GetBytes (postData);
			request.ContentType = "application/x-www-form-urlencoded";
			//request.ContentLength = byteArray.Length; // Uncomment this to fix the wait
			Stream dataStream = request.GetRequestStream ();
			dataStream.WriteAsync (byteArray, 0, byteArray.Length).Wait ();
			var resp = request.GetResponse ();
			Console.WriteLine (((HttpWebResponse) resp).StatusDescription);

		static HttpListener CreateListener (Action<HttpListenerContext> contextAssert, string port)
			string TestHost, LocalServer;

			TestHost = "localhost:" + port;
			LocalServer = string.Format ("http://{0}/", TestHost);

			var l = new HttpListener ();
			l.Prefixes.Add (string.Format ("http://+:{0}/", port));
			l.Start ();
			l.BeginGetContext (ar => {
				var ctx = l.EndGetContext (ar);

				try {
					if (contextAssert != null)
						contextAssert (ctx);
				} finally {
					ctx.Response.Close ();
			}, null);

			return l;
Comment 3 Marek Safar 2012-04-30 10:39:13 UTC
.NET output

Comment 4 Gonzalo Paniagua Javier 2012-04-30 14:19:27 UTC
This is a new .NET4.0+ behavior.
Fixed in master/8e67b8c2
Comment 5 Gonzalo Paniagua Javier 2012-05-22 05:43:04 UTC
*** Bug 5210 has been marked as a duplicate of this bug. ***
Comment 6 Gonzalo Paniagua Javier 2012-05-22 05:43:26 UTC
Backported to mono-2-10/f537b39