[Mono-list] HttpWebRequest and Cookies, does not work as expected

fekberg ekberg.filip at gmail.com
Mon Nov 14 02:52:50 EST 2011


Consider that I have the following code ( from a Reddit C# API ):

        var redditCookie = new CookieContainer();
        HttpWebRequest login =
WebRequest.Create("http://www.reddit.com/api/login/" + user) as
        login.CookieContainer = redditCookie;
        login.Method = "POST";
        login.ContentType = "application/x-www-form-urlencoded";

        string postData = string.Format("api_type=json&user={0}&passwd={1}",
user, pswd);
        byte[] dataBytes = ASCIIEncoding.ASCII.GetBytes(postData);
        login.ContentLength = dataBytes.Length;
        Stream postStream = login.GetRequestStream();

        postStream.Write(dataBytes, 0, dataBytes.Length);

        WebResponse response = login.GetResponse();
        StreamReader r = new StreamReader(response.GetResponseStream());

This request works out great, I get a response with my auth-part on the last
line, but if I do this right after, the cookie is not found:

        jsonGet.Headers["COOKIE"] = redditCookie.GetCookieHeader(new
System.Uri("http://www.reddit.com/api/login/" + usr));

Running this in Visual Studio on Windows will work as I expect, but when I
move this to a Linux-machine running Mono 2.10 it does not, the
GetCookieHeader-call returns null which is Quite strange.

If I cast the WebResponse to a HttpWebResponse I can see that the response
sends me some cookie values for me to set, but it's like they are ignored.

Any ideas?

// Filip

View this message in context: http://mono.1490590.n4.nabble.com/HttpWebRequest-and-Cookies-does-not-work-as-expected-tp4038696p4038696.html
Sent from the Mono - General mailing list archive at Nabble.com.

More information about the Mono-list mailing list