I have confirmed the behaviour is different in .NET 3.5 compared to .NET 4.0. Is used the following code to test:
Uri sourceUri = new Uri(@"http://www.html-kit.com/tools/cookietester/");
WebClientEx webClientEx = new WebClientEx();
webClientEx.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
webClientEx.UploadString(sourceUri, "cn=MyCookieName&cv=MyCookieValue");
var text = webClientEx.DownloadString(sourceUri);
var doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(new MemoryStream(Encoding.ASCII.GetBytes((text))));
var node = doc.DocumentNode.SelectNodes("//div").Single(n => n.InnerText.StartsWith("\r\nNumber of cookies received:"));
Debug.Assert(int.Parse(node.InnerText.Split(' ')[4]) == 1);
Of course, this doesn't answer your question; but I can see no reason why there is a difference in behaviour other than to say maybe it was fixed in .NET 4.0 and the fix hasn't be put into .NET 3.5 or prior versions.
I tried a similar thing with HttpWebRequest
and had the same problem (works in 4, but not prior):
HttpWebRequest webreq = ((HttpWebRequest) (WebRequest.Create(sourceUri)));
CookieContainer cookies = new CookieContainer();
var postdata = Encoding.ASCII.GetBytes("cn=MyCookieName&cv=MyCookieValue");
webreq.CookieContainer = cookies;
webreq.Method = "POST";
webreq.ContentLength = postdata.Length;
webreq.ContentType = "application/x-www-form-urlencoded";
Stream webstream = webreq.GetRequestStream();
webstream.Write(postdata, 0, postdata.Length);
webstream.Close();
using (WebResponse response = webreq.GetResponse())
{
webstream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(webstream))
{
String responseFromServer = reader.ReadToEnd();
var doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(new MemoryStream(Encoding.ASCII.GetBytes((responseFromServer))));
var node =
doc.DocumentNode.SelectNodes("//div").Single(n => n.InnerText.StartsWith("\r\nNumber of cookies received:"));
Debug.Assert(int.Parse(node.InnerText.Split(' ')[4]) == 1);
}
}
So, there seems to be a problem with HttpWebRequest
(which WebClient
uses). This might be new because I've seen people use code like this before 4.0 was released (maybe prior to 3.50 and they say it worked.
If it's urgent, I would suggest contacting Microsoft Support. If you have an MSDN license the following link will detail how to make a support request with the included MSDN support tickets: http://msdn.microsoft.com/en-us/subscriptions/bb266240.aspx If you don't have MSDN you can contact Support as detailed here: https://support.microsoft.com/oas/default.aspx?Gprid=8291&st=1&wfxredirect=1&sd=gn
if it's less urgent, then you could probably log the issue at http://connect.microsoft.com/VisualStudio to see if you get a response with workarounds.