Get the cookie code from the web-request

First Mandc 1 Reputation point

I tried the following method to log into Facebook to get cookies, but I couldn't get cookies.
What is the problem that occurred and how is it resolved.

Dim cookieJar As New Net.CookieContainer()
            Dim username As String = "username"
            Dim password As String ="password"
            Dim cookieContainer As CookieContainer
            Dim bytes As Byte()
            Dim httpWebRequest As HttpWebRequest
            Dim s As String = "lsd=AVpRTdGdsWI&jazoest=2975&m_ts=1647910928&li=ECA5YmmANHURTJaXGqilTsJc&try_number=0&unrecognized_tries=0&email=" & username & "&pass=" & password & "&login=%D8%AA%D8%B3%D8%AC%D9%8A%D9%84+%D8%A7%D9%84%D8%AF%D8%AE%D9%88%D9%84&bi_xrwh=0"
            cookieContainer = New CookieContainer()
            bytes = utf8Encoding.GetBytes(s)
            httpWebRequest = CType(WebRequest.Create(""), HttpWebRequest)
            httpWebRequest.Method = "POST"
            httpWebRequest.Proxy = Nothing
            httpWebRequest.Host = ""
            httpWebRequest.Headers.Add("Upgrade-Insecure-Requests", "1")
            httpWebRequest.ContentType = "application/x-www-form-urlencoded"
            httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:59.0) Gecko/20100101 Firefox/59.0"
            httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

            httpWebRequest.CookieContainer = cookieContainer
            httpWebRequest.Referer = ""
            httpWebRequest.KeepAlive = True
            httpWebRequest.ContentLength = CLng(bytes.Length)
            Dim requestStream As Stream = httpWebRequest.GetRequestStream()
            requestStream.Write(bytes, 0, bytes.Length)
            httpWebResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)

            Dim streamReader As StreamReader = New StreamReader(httpWebResponse.GetResponseStream())
            Dim text As String = streamReader.ReadToEnd()
             RichTextBox1.Text = text
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,595 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 26,996 Reputation points Microsoft Vendor

    Hi @First Mandc
    You can refer to the sample code in the documentation below to get cookies.
    HttpWebRequest.CookieContainer Property
    Also see answers in this question.
    Get cookies from httpwebrequest
    Best Regards.
    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments