[Mono-list] System.Net.ProtocolViolationException: Content-Length not set at System.Net.HttpWebRequest.CommonChecks

Timothy Parez timothyparez at gmail.com
Thu Apr 3 07:09:45 EDT 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

The following code works fine on a Windows Server:

private string Authenticate()
{
	//Create a web request for the Google ClientLogin service
	HttpWebRequest authRequest = (HttpWebRequest)HttpWebRequest.Create("https://www.google.com/accounts/ClientLogin 
");
	authRequest.KeepAlive = false;
	authRequest.ContentType = "application/x-www-form-urlencoded";
	authRequest.Method = "POST";
	authRequest.AllowWriteStreamBuffering = false;

	//Build the POST data
	StringBuilder postBuilder = new StringBuilder();
	postBuilder.AppendFormat("accountType={0}", GOOGLE_ACCOUNT_TYPE);
	postBuilder.AppendFormat("&Email={0}", GOOGLE_USERNAME);
	postBuilder.AppendFormat("&Passwd={0}", GOOGLE_PASSWORD);
	postBuilder.AppendFormat("&service={0}", GOOGLE_SERVICE);
	postBuilder.AppendFormat("&source={0}", GOOGLE_SOURCE);

	//Convert the POST data to a byte[]
	byte[] data = ASCIIEncoding.ASCII.GetBytes(postBuilder.ToString());
	authRequest.ContentLength = data.Length;

	//Get the request stream and POST the data
	Stream requestStream = authRequest.GetRequestStream();
	requestStream.Write(data, 0, data.Length);
	requestStream.Close();

	//Get the response
	HttpWebResponse authResponse =  
(HttpWebResponse)authRequest.GetResponse();
	Stream responseStream = authResponse.GetResponseStream();
	StreamReader responseReader = new StreamReader(responseStream);
	string response = responseReader.ReadToEnd();

	responseStream.Close();
		
	if (response.Contains("Auth"))
	{
		int index = response.LastIndexOf("=") + 1;
		string auth = response.Substring(index, response.Length - index);
		return auth;
	}
	else
	{
			throw new Exception(response);
	}
}

On Linux/Mono 1.9 however, it throws an exception:
System.Net.ProtocolViolationException: Content-Length not set at  
System.Net.HttpWebRequest.CommonChecks (Boolean putpost) [0x0008d] in / 
usr/src/mono1.9-final/mono-1.9/mcs/class/System/System.Net/ 
HttpWebRequest.cs:651 at  
System.Net.HttpWebRequest.BeginGetRequestStream (System.AsyncCallback  
callback, System.Object state) [0x000ab] in /usr/src/mono1.9-final/ 
mono-1.9/mcs/class/System/System.Net/HttpWebRequest.cs:663 at  
System.Net.HttpWebRequest.GetRequestStream () [0x00000] in /usr/src/ 
mono1.9-final/mono-1.9/mcs/class/System/System.Net/HttpWebRequest.cs: 
715 at GoogleDocumentManager.Authenticate () [0x000aa] in /tmp/www- 
data-temp-aspnet-0/b9f31429/App_Web_4abb242d_4.cs:281 at  
GoogleDocumentManager.FetchData (System.String Url) [0x00015] in /tmp/ 
www-data-temp-aspnet-0/b9f31429/App_Web_4abb242d_4.cs:234


I've checked the ContentLength and data.Length values, they are both  
equal.

Any ideas?


Timothy.



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFH9Lr5+j5y+etesF8RAjGMAJwKBLlOK9prbkE0iKPYWOiBUP9EGQCgnlFJ
rnkTrPyLQl4UNZX/Bg5JPCo=
=/d4Y
-----END PGP SIGNATURE-----


More information about the Mono-list mailing list