0

I am working an a VB.NET console application which automatically downloads textfiles from different websites. So far I've been using

My.Computer.Network.DownloadFile(url, local_path, username, password)

and it works fine - but there is one website which uses cookies. Under normal circumstances I have to manually visit the website in my browser, login by entering my credentials into a login form, press the "Log in" button and then there is a link which lets me download the file. If I use this link in my normal download function, I always receive a message "Not logged in." so this is basically my problem: I need to call this URL with a WebClient or HttpWebRequest and log in using my credentials before I tell it to download the file.

However, I can not find a good example or guideline anywhere for this problem. So far, I've tried the following:

Dim client As New WebClient
Dim values As New NameValueCollection()
values.Add("login[username]", "<my_login>")
values.Add("login[password]", "<my_password>")
client.UploadValues("https://www.website.com/customer/account/login/", values)
Dim strDL As String = client.DownloadString("https://www.website.com/downloadFile.php/")
Console.WriteLine(strDL)

This isn't working and my console just puts out "Not logged in."

I know that you can also work with a CookieContainer but not with WebClient, only with HttpWebRequest. I guess I didn't grasp the concept of that yet. In my case, the cookies for the website expire after 1 hour so I assume that my approach with the NameValueCollection also would not work (or maybe it would but only as long as the cookie is valid) so my guess is that I have to somehow log into the website, get the cookie names and values and then use those information to download the file. However, as I said, I can not find a good example on how to do this anywhere.

  • Read the notes here: [Understand HttpWebRequest in KeepAlive mode](https://stackoverflow.com/a/49609131/7444103). There's also a Web Login procedure that is used to test the different states of a WebRequest, to, e.g., handle a `200 OK` redirection when you first request a resource that is handed over only after a login procedure is completed and how to handle the following `301`-`303` redirection(s) to the landing page. -- You need of course a CookieContainer but you also need to send all elements of the form, hidden included, Url-Encoded. – Jimi Jan 14 '21 at 12:19
  • *can't use cookies in webclient* - https://stackoverflow.com/questions/14551345/accept-cookies-in-webclient which also leads to https://stackoverflow.com/questions/2825377/how-can-i-get-the-webclient-to-use-cookies ? – Caius Jard Jan 14 '21 at 13:47

0 Answers0