0

I have been through many articles here on StackOverflow and on Microsoft's own website, but I can't seem to find the correct, working solution.

I make a request to a website, which it returns two cookies. I can get those cookies, as shown below. I save the cookies into a CookieCollection (cookieBam). Since that site requires the cookies to be called again to get the actual content, I re-request the site, adding what I could find on how to send the cookies properly so that when I re-request the site, I get the original response, just like the cookies were never sent.

Please keep in mind when answering that I don't create the HttpClient in here, as this code is inside a background worker (of which I could have up to six working at any time). And from what I have read, there should only be one instance of the HttpClient.

Here is the code in question:

If cookieBam.Count = 0 Then
    webRequest = New HttpRequestMessage(HttpMethod.Get, sLink)
    webResponse = Client.Send(webRequest)

    Dim uriLink As Uri = New Uri(sLink)

    For Each sCookie In webResponse.Headers.GetValues("Set-Cookie")
        cookieBam.SetCookies(uriLink, sCookie)
    Next

    inStream = New StreamReader(webResponse.Content.ReadAsStream)
    sSource = inStream.ReadToEnd.Trim

    inStream.Close()
    webRequest.Dispose()
End If

iStart = sSource.IndexOf("image-loader")

If iStart = -1 Then
    webRequest = New HttpRequestMessage(HttpMethod.Get, sLink)

    sCookie = ""
    For Each cCookie In cookieBam.GetAllCookies
        sCookie += cCookie.Name & "=" & cCookie.Value & "; "
    Next

    webRequest.Headers.Add("Cookie", sCookie.Substring(0, sCookie.Length - 2))

    webResponse = Client.Send(webRequest)
End If

Am I not sending the cookies correctly?

  • Do the answers to [How do I set a cookie on HttpClient's HttpRequestMessage](https://stackoverflow.com/questions/12373738/how-do-i-set-a-cookie-on-httpclients-httprequestmessage) help? – Andrew Morton Sep 01 '23 at 08:36
  • No it doesn't. As I stated in my question, there are multiple workers that access this code. Therefore, the cookie must be sent on an individual request and not with the httpclient. – Gary L Smith Sep 01 '23 at 17:44
  • "No it doesn't" - using "it" says that you did not look at any answers other than the accepted answer. Just to be clear, did you? Some people don't, do it's best to be sure. – Andrew Morton Sep 01 '23 at 21:32
  • I spent a total of 12 hours going over this website, other vb forums and microsoft's website without any help (in fact MS's site is complete worthless on guidance). I don't like to post on here because of some of the negative feedback I have received in the past, but I do look past that and when I truly need help, I ask. I am a rather gifted developer (at least I was in the past) and when I get this problem licked (and other), I would like to be able to write updates on some dated code on here and elsewhere. But for now, I need help with this issue because it does not make sense. – Gary L Smith Sep 02 '23 at 01:23
  • And please keep in mind that I cannot add the cookies to the HttpClient, as I have only ONE client, but I may be making up to 7 requests at the same time (through background workeres). Therefore, the cookies must be sent with the HttpRequestMessage. I can collect the cookies. That is not a problem. It's sending them back so the server things I made my first pass at the site and doesn't want to reminded me of its useless ads and pass me onto the content that I am after. Make sense? – Gary L Smith Sep 02 '23 at 01:34
  • As you need more than one HttpClient, [Make HTTP requests using IHttpClientFactory in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0) would be the way to go, as indicated in [this answer](https://stackoverflow.com/a/64157842/1115360). – Andrew Morton Sep 02 '23 at 12:56

0 Answers0