Bug 1550 - HttpWebRequest behavior differ from "pure" .NET
Summary: HttpWebRequest behavior differ from "pure" .NET
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Martin Baulig
Depends on:
Reported: 2011-10-18 13:51 UTC by TheCoachFr
Modified: 2017-07-07 18:26 UTC (History)
6 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 TheCoachFr 2011-10-18 13:51:21 UTC

I'm trying to implement a request to a web site with login in order to retrieve some html data. 

I have a bunch of code that actually works well in .NET (I means I can login to the web site and have the expect result). 
When I try the exact same code with monotouch (from the same computer) the request never works. 

Basically I do this : 
           public static void Main() 
           CredentialCache cCache = new CredentialCache(); 
           var cookies = new CookieContainer(); 
           Connect("http://myWebSite:90/", cCache, cookies); 
           Connect("http://myWebSite:90/login.php", cCache, cookies); 
           Connect("http://myWebSite:90/settings.php", cCache, cookies); 

       private static void Connect(String url, CredentialCache 
cCache, CookieContainer cookies) 
           HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
           // Set the Method property of the request to POST. 
           request.Method = "POST"; 
           // Create POST data and convert it to a byte array. 
           string postData = "login=login&passwd=passwd"; 
           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; 
           request.CookieContainer = cookies; 
           request.Accept = 
           request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64)AppleWebKit/535.1(KHTML, like Gecko) Chrome/13.0.782.112Safari/535.1"; 
           request.Credentials = cCache; 
           request.AllowAutoRedirect = false; 
           request.KeepAlive = true; 
           request.MaximumAutomaticRedirections = 50; 
           // Get the request stream. 
           Stream dataStream = request.GetRequestStream(); 
           dataStream.Write(byteArray, 0, byteArray.Length); 
           // Get the response. 
           HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
           cookies.Add(new Uri("http://myWebSite:90/settings.php"), response.Cookies); 
           cookies.Add(new Uri("http://myWebSite:90/login.php"), response.Cookies); 
           cookies.Add(new Uri("http://myWebSite:90/"), response.Cookies); 
           // Get the stream containing content returned by the server. 
           dataStream = response.GetResponseStream(); 
           StreamReader reader = new StreamReader(dataStream); 
           string responseFromServer = reader.ReadToEnd(); 
           // Display the content. 
           Console.WriteLine("resp = " + responseFromServer); 
           // Clean up the streams. 

I launch that sample with --trace=E:all without any new exceptions.

PS: I had some redirect trouble that why I handle it by my self. 

When it works (with .NET on windows) that code display some html code. 
With mono the login process never succeed and of course I have no answer. 
I have the latest mono version (downloaded yesterday). 

I real don't understand why I can't make mono works with my code ... 

Any ideas ? 

Thanks for you help 

Comment 1 Gonzalo Paniagua Javier 2011-10-20 21:11:13 UTC
Do you have a web site that I can use to test this code?
Comment 2 TheCoachFr 2011-10-21 03:00:28 UTC

It could be possible, but the full story is : I have an iOS app to remotely configure my DSL box ("free" french provider), they recently allow outside access to configure it. 
So if I open access to that website I will allow the people who connect to totally change/break my DSL/phone/TV connections ...
So: I can accept to open the connection but only if its for a certain amount of time and for a limited number of trusted people ...


Comment 3 Gonzalo Paniagua Javier 2011-10-22 18:15:59 UTC
I forgot to ask which version of Mono you are using. Can you reproduce this with the latest 2.10.6 and the latest MonoTouch 5.0?

If so, we can arrange meeting on IRC, gtalk or similar.
Comment 4 TheCoachFr 2011-10-24 13:46:43 UTC
I have the latest versions.

We can have a meeting via talk (alexandre.fau@gmail.com). We just need to find compatible hours regarding our timezone :-) (Paris time for me)

Comment 5 Gonzalo Paniagua Javier 2012-01-10 17:13:50 UTC
Is this still a problem?
Comment 6 TheCoachFr 2012-01-11 16:23:51 UTC
If nothing hs been fixed since yes it is.
I will check with latest version and i keep your inform.


Comment 7 TheCoachFr 2012-01-12 16:15:30 UTC

So I test with the latest versions and It's still a problem.

So I can give you the IP address and credential to access the website but not in a public way.
How can I sent them to you (private manner) ?


Comment 8 Gonzalo Paniagua Javier 2012-01-12 17:57:44 UTC
Send it to gonzalo@xamarin.com.
Comment 9 Gonzalo Paniagua Javier 2012-01-17 00:39:47 UTC
My attempts to quickly fix this problem break our existing tests and I am not sure those tests are still good...

In the meantine, as a workaround, instead of adding different cookies for different domains, do this:

           // Get the response. 
           HttpWebResponse response = (HttpWebResponse)request.GetResponse();
           foreach (Cookie c in response.Cookies) {
                   cookies.Add(new Cookie (c.Name, c.Value, "/", c.Domain));

The code will then work fine with MS and Mono.
Comment 10 Rodrigo Kumpera 2014-01-12 01:46:12 UTC
Martin, this bug might of interest to you.
Comment 11 Alexander Kyte 2015-03-02 10:51:20 UTC
I'm marking this bug as confirmed, since it appears that Gonzalo can reproduce.
Comment 12 Martin Baulig 2016-11-11 09:58:01 UTC
Did we ever receive a reproducible test case for this?
Comment 13 Ludovic Henry 2017-07-07 18:26:06 UTC
Out of Gonzalo, we never got a repro case, and he is not with Xamarin at this point. If you can still hit it with latest mono version, please feel free to reopen. Thank you.