1

As the title says. I'm trying to edit an inherited instance of either the HttpClientHandler, HttpClient or CookieContainer that every time the .Add from CookieContainer is called by the HttpClient (after receiving them through automatic requests) to edit various proportions of the Cookie/Cookies, such as expiration date and so.

Of course, the Add sub of the CookieContainer is just an example, if I can manipulate what I want through any other function, it won't make any difference.

I need to have each cookie edited before/while being added, not after (manual iteration). That is my goal. As long as everything is done automatically and not having to call something to iterate through each cookie and edit them, any answer is welcomed.

Example:

Public Class CustomCookieContainer
Inherits Net.CookieContainer

Protected Overrides Sub OnAdd(cookie as Cookie)
  cookie.Name = $"BlahBlah-{cookie.name}"
  cookie.ExpireDate = DateTime.Now.AddYear(2185816)
  Add(cookie, true)
End Sub

End Class
Dezv
  • 156
  • 1
  • 8

1 Answers1

0

do you mean something like this?

For Each coookie In cookieContainer.GetCookies(Uri)
  If coookie.ToString().Contains("XSRF-TOKEN") Then
     'do stuff here like:
     Dim CookieLenght As Integer = coookie.ToString().Length
     Dim c() As Char = coookie.ToString().ToCharArray(11, CookieLenght - 11)
     Dim d As String = New String(c)
     request.Headers.Add("X-XSRF-TOKEN", c)
     request.Headers.Add("DNT", "1")
  End If
Next
Gadzin
  • 73
  • 7
  • 1
    No. I just want to rename them and edit the expiration date of every cookies before they are being added (which are automatically added by the httpclient) into the CookieContainer. A hard code that will do so and handle every cookie automatically is what I want, I don't want to iterate through each cookies and change the values after each requests. I want them to be processed on the fly. – Dezv Oct 05 '19 at 14:58
  • 1
    Added an example. – Dezv Oct 05 '19 at 15:02
  • 1
    @Ebiツ I don't think you can manipulate cookie before you will get response cookie name. For setting date you use `Response.Cookies("cookiename").Expires = DateTime.Now.AddDays(1)` but i'm not an expert. There's a link for cookiemanipulation [link](https://learn.microsoft.com/en-us/previous-versions/ms178194(v=vs.140)?redirectedfrom=MSDN#writing-cookies) – Gadzin Oct 05 '19 at 18:16
  • 2
    Yeah, I know about this. But I've asked this question just in case there is something that I'm missing. Thank you! – Dezv Oct 05 '19 at 18:57