Michal
Michal

Reputation: 343

Getting Cookies from HTTP response

I have problem getting cookies from HTTP response. Im sure, that response should have cookies, but I cant see them in my app.

Here is my code:

    private static CookieContainer cookies = new CookieContainer();
    private static CookieContainer Cookies
    {
        get
        {
            return cookies;
        }
    }

    public static async Task<HttpStatusCode> SendPostRequest(string url, string postData)
    {
        if (url == null)
            throw new ArgumentNullException("url");

        if (postData == null)
            throw new ArgumentNullException("postData");

        HttpStatusCode statusCodeToReturn = HttpStatusCode.Forbidden;
        HttpWebRequest webRequest = HttpWebRequest.CreateHttp(url);
        webRequest.Method = "POST";
        var cookies = Cookies;
        webRequest.CookieContainer = cookies;
        //webRequest.SupportsCookieContainer = true;
        using (var requestStream = await webRequest.GetRequestStreamAsync())
        {
            var bytes = Encoding.UTF8.GetBytes(postData);
            requestStream.Write(bytes, 0, bytes.Length);
        }

        using (WebResponse response = await webRequest.GetResponseAsync())
        {
            statusCodeToReturn = WebResponseToHTTPStatusCode(response);
        }

        return statusCodeToReturn;
    }

Cookies (using Wireshark):

rack.session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiJFMzg1ZjYxNzIzNzQ4MmY5NmI3NTMw%0AYWMwZmRjNmVmZjMwMDk4OTgzZGUwNjRlNzIzODlmODNjYzE2YmVmMjNlOQ%3D%3D%0A--30d79cd2276c3236de11104852bba4b84bf80f26; path=/; HttpOnly

Upvotes: 1

Views: 1354

Answers (2)

Michal
Michal

Reputation: 343

The problem is in returned Cookies. Cookies without set DOMAIN are NOT supported in WP7.

Upvotes: 1

sedgwickz
sedgwickz

Reputation: 59

I think you can just create an global variable to save the cookie.Such as in your app.xaml.cs file you can create a variable like this:

public CookieContainer GlobalCookie{get;set;}

And make the GloalCookie equal to your successful HttpWebRequest CookieContainer.

Then you can use this variable when you call another API.Hope to help you:)

Upvotes: 0

Related Questions