Bug 7424 - HTTPWebRequest CookieContainer support not working in MonoTouch 6.0 (vs 5.4)
Summary: HTTPWebRequest CookieContainer support not working in MonoTouch 6.0 (vs 5.4)
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.0.x
Hardware: Macintosh Mac OS
: High critical
Target Milestone: Untriaged
Assignee: Martin Baulig
URL:
Depends on:
Blocks:
 
Reported: 2012-09-24 15:56 UTC by mscoder610
Modified: 2014-01-28 09:28 UTC (History)
10 users (show)

Tags:
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 Developer Community or GitHub 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:
Status:
RESOLVED FIXED

Description mscoder610 2012-09-24 15:56:16 UTC
After upgrading to MonoTouch 6.0 (from 5.4), I noticed an issue with getting cookies from the Cookies property on an HttpWebResponse. This worked correctly in MonoTouch 5.4.

When using the following code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://myserver:8080/Test"));
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Cookie Count: " + response.Cookies.Count);

With a server outputting the following response for that URI:
HTTP/1.1 200 OK
Content-Length: 2
Content-Type: text/plain
Set-Cookie: MyKey=MyValue; path=/;

MonoTouch 6.0 will output "Cookie Count: 0".
MonoTouch 5.4 (and desktop .NET 4.0 on Windows) will output "Cookie Count: 1".
Comment 1 Sebastien Pouliot 2012-09-24 16:02:41 UTC
Martin, I think there were some recent change in this area between 5.4 and 6.0.
Comment 2 mscoder610 2012-09-26 15:31:59 UTC
It looks like there's another related issue (this one exists in MonoTouch 6.0.1) -

If you do the same test above, but the URI looks like "http://myserver.mycompany.com:8080/Test", you won't even get past the request.GetResponse() line. The call will cause a cookie exception -

System.Net.WebException: Invalid cookie domain: myserver.mycompany.com ---> System.Net.CookieException: Invalid cookie domain: myserver.mycompany.com
  at System.Net.CookieContainer.AddCookie (System.Net.Cookie cookie) [0x00060] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/CookieContainer.cs:144 
  at System.Net.CookieContainer.Add (System.Uri uri, System.Net.Cookie cookie) [0x0003b] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/CookieContainer.cs:263 
  at System.Net.HttpWebResponse.SetCookie (System.String header) [0x00094] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebResponse.cs:363 
  at System.Net.HttpWebResponse.FillCookies () [0x00023] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebResponse.cs:337 
  at System.Net.HttpWebResponse..ctor (System.Uri uri, System.String method, System.Net.WebConnectionData data, System.Net.CookieContainer container) [0x000af] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebResponse.cs:89 

Again, the same code works without any issues on desktop .NET 4.0 on Windows, or the MonoTouch 5.4 series.
Comment 3 Brian Langer 2012-09-26 18:39:24 UTC
Hi everyone,
I'm having a similar problem. 
After updating from monotouch 5.4 straight to 6.0.1, I stop receiving cookies.
In my case I'm using UploadValuesAsync, so when the UploadValuesCompleted gets triggered, the CookieContainer is empty.

I tested it with Wireshark and I'm receiving a cookie in the response, but the cookie wont reach the UploadValuesCompleted. 

To sum up, the rest of the response looks ok: I still receive the json response that I'm expecting at the UploadValuesCompleted registered method, but no cookie.

Thanks in advance,
Comment 4 Martin Baulig 2012-09-26 20:57:35 UTC
This may be related to my cookie changes.  I am looking at the issue now.
Comment 5 Martin Baulig 2012-09-26 21:34:52 UTC
I identified the problem and should have a fix by the end of the night.
Comment 6 Brian Langer 2012-09-26 21:48:26 UTC
I'm very glad to read that, thanks a lot Martin.

Regards,
Comment 7 Martin Baulig 2012-09-26 22:12:52 UTC
Fixed; master commit f7cf18e; mono-2-10 commit 8182c8c.
Comment 8 Ben 2013-04-03 21:57:04 UTC
I have an issue with
System.Net.CookieException: Invalid cookie domain
Using Mono 2.10.12 (mono-2-10/c9b270d)

I create HttWebRequest with AllowAutoRedirect=true, the server responses with 302 and redirects to another subdomain which sets cookie. So original request goes to domainA.company.com which redirects to domainB.copmany.com and cookies have domain domainB.company.com

The code works fine on desktop .NET but mono throws System.Net.CookieException: Invalid cookie domain.
Comment 9 Luigi Saggese 2013-06-23 06:14:40 UTC
I have same exception:

problem WebException: Invalid cookie domain: mysubdomain.domain.com
	  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:852 
  at System.Net.HttpWebRequest.GetResponse () [0x0000e] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:860 

I have read a CookieCollection from first request and i have add to my CookieContainer in the second request as like this
string homepage = "http://mysubdomain.domain.com";
			HttpWebRequest requests = WebRequest.Create(homepage) as HttpWebRequest;  
			requests.Method = "GET";
			requests.Accept = "application/json";
			requests.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0";
			using (HttpWebResponse response = requests.GetResponse() as HttpWebResponse) {  
				// Get the response stream  

				CookieToUse = response.Cookies;
			}
string address = "http://mysubdomain.domain.com/test/api/

HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;  
			request.Method = "GET";
			request.Accept = "application/json";

			request.AllowAutoRedirect =true;
			request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0";
			request.CookieContainer = new CookieContainer ();
			request.CookieContainer.Add(CookieToUse);