'Login to a Django site using a POST request

I'm writing a VB.NET program that needs to send data to a Django site, but the site is password protected. Therefore I need to log in first. The code I'm using right now is:

Dim reqT As HttpWebRequest
Dim resT As HttpWebResponse
reqT = WebRequest.Create("http://websiteURL/login/")
reqT.Method = "POST"

Dim Data As String
Data = "{""username"":""fakeusername""&""password"":""fakepassword""}"
Dim credArray As Byte() = Encoding.UTF8.GetBytes(Data)
reqT.ContentType = "application/x-www-form-urlencoded"
reqT.ContentLength = credArray.Length
Dim dataStreamT As Stream = reqT.GetRequestStream()
dataStreamT.Write(credArray, 0, credArray.Length)
dataStreamT.Close()

This is returning a 403 error from the webserver, unfortunately I'm not sure how to fix it. I think it might be the csrf failing or I might be formatting my POST data wrong. Any help would be appreciated.



Solution 1:[1]

Sounds like CSRF protection to me. The Django CSRF documentation is here:

As per the How It Works section, your POST request will need to pass along the CSRF token, and the CSRF cookie.

If you make a GET request to the login page, you should be able to grab the token from the HTML source. I’m guessing you’d also have access to the cookie?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1