-1

How can I get cookies from the HttpResponseMessage?

I'm getting an Index out of bound exception when executing following code. Not sure where the problem is:

using System;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Net;

namespace ConsoleApp1
{
    internal class Program
    {

        static HttpClientHandler handler = new HttpClientHandler();
        static HttpClient client = new HttpClient(handler);
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello, World!");          
            await ReadCookies();
            Thread.Sleep(15000);
        }

        static async Task ReadCookies()
        {            
            for (int i = 0; i < 10; i++)
            {
                client.BaseAddress = new Uri("http://google.com");                
                var request = new HttpRequestMessage(HttpMethod.Get, "index.html");
                var response = await client.SendAsync(request);
                var cks = GetCookies(response);
                Console.WriteLine(await response.Content.ReadAsStringAsync());
                Thread.Sleep(1000);
                foreach (Cookie ck in cks)
                {
                    Console.WriteLine($"{ck.Name}:{ck.Value}");
                }                
            }
        }

        static CookieCollection GetCookies(HttpResponseMessage message)
        {
            var cookies = new CookieCollection();
            var setCookie = Enumerable.Empty<string>();
            if (message.Headers.TryGetValues("Set-Cookie", out setCookie))
            {
                foreach (var cookieStr in setCookie)
                {
                    foreach (var cookieToken in cookieStr.Split(';'))
                    {
                        var keyValueTokens = cookieToken.Split('=');                        
                        var cookie = new Cookie((keyValueTokens[0]).Trim(), (keyValueTokens[1]).Trim());
                        cookies.Add(cookie);
                    }
                }
            }
            return cookies;
        }
    }

}
Dan
  • 11,077
  • 20
  • 84
  • 119
  • 1
    Probably there are no data for `keyValueTokens[]` because `cookieToken` doesn't contain '=' when splitting. Have you tried debugging? – ɐsɹǝʌ ǝɔıʌ Feb 23 '23 at 15:46
  • 2
    You probably have a cookie value that looks like this `A=B; C=D; E; F=G` so splitting on `=` doesn't always give you two values. This will always happen with secure or HttpOnly cookies for example. But didn't you try stepping through your code and debugging it? This seems pretty simple to figure out. – DavidG Feb 23 '23 at 15:50
  • Yes, the problem was with cookies that did not contain =. Thanks everyone. – Dan Feb 23 '23 at 18:55

1 Answers1

0

This should take care of most of edge cases:

public static CookieCollection GetCookies(HttpResponseMessage message)
    {
        var cookies = new CookieCollection();
        if (message == null)
            return cookies;
        var setCookie = Enumerable.Empty<string>();
        if (message.Headers.TryGetValues("Set-Cookie", out setCookie))
        {
            foreach (var cookieStr in setCookie)
            {
                foreach (var cookieToken in cookieStr.Split(';'))
                {
                    var name = cookieToken.Trim();
                    var value = "";
                    if (cookieToken.Contains('='))
                    {
                        var keyValueTokens = cookieToken.Split('=');
                        name = (keyValueTokens[0]).Trim();
                        if (keyValueTokens.Length > 1 && !string.IsNullOrEmpty(keyValueTokens[1]))
                        {
                            value = (keyValueTokens[1]).Trim();
                        }
                    }
                    if (!string.IsNullOrEmpty(name))
                    {
                        var cookie = new Cookie(name, value);
                        cookies.Add(cookie);
                    }
                }
            }
        }
        return cookies;
    }
Dan
  • 11,077
  • 20
  • 84
  • 119