Get the cookie code from the web-request

First Mandc 1 Reputation point
2022-03-22T23:33:53.897+00:00

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("https://mbasic.facebook.com/login.php?refsrc=https%3A%2F%2Fmbasic.facebook.com%2F&lwv=100&login_try_number=1&refid=8"), HttpWebRequest)
            httpWebRequest.Method = "POST"
            httpWebRequest.Proxy = Nothing
            httpWebRequest.Host = "mbasic.facebook.com"
            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 = "https://mbasic.facebook.com/login/?next&ref=dbl&fl&refid=8"
            httpWebRequest.KeepAlive = True
            httpWebRequest.ContentLength = CLng(bytes.Length)
            Dim requestStream As Stream = httpWebRequest.GetRequestStream()
            requestStream.Write(bytes, 0, bytes.Length)
            requestStream.Close()
            httpWebResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)
            cookieContainer.Add(httpWebResponse.Cookies)

            Dim streamReader As StreamReader = New StreamReader(httpWebResponse.GetResponseStream())
            Dim text As String = streamReader.ReadToEnd()
            streamReader.Close()
             RichTextBox1.Text = text
Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-03-23T06:35:13.177+00:00

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.