1

Unable to download .csv file after logging in using WebClient

My code looks like

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim client As New Net.WebClient
    Dim loginInfo As New Specialized.NameValueCollection
    loginInfo.Add("email", "admin@domain.com")
    loginInfo.Add("password", "12345")
    client.UploadValues("https://www.domain.com/login.asp", "POST", loginInfo)
    client.DownloadFile("https://www.domian.com/report.asp?DataFile=97YD7Z9G55.csv", "report.csv")
  End Sub
End Class

instead of actual data file it saves login page as report.csv

I didn't find following link helpful

Using WebClient to login and download files

Community
  • 1
  • 1
AZee
  • 520
  • 1
  • 6
  • 22
  • So you tried the cookie-aware web client used in the answer to that question and… what happened? – Ry- Aug 25 '13 at 22:07
  • Yes I tried it but no success at all – AZee Aug 25 '13 at 22:25
  • That is absolutely unhelpful. What *happened*? How did you try using it? – Ry- Aug 25 '13 at 22:29
  • I copied the class from http://stackoverflow.com/questions/2825377/how-can-i-get-the-webclient-to-use-cookies and replaced line#3 of my code with this Dim client As New CookieAwareWebClient – AZee Aug 25 '13 at 22:32

1 Answers1

0

One possible problem is that your login request is not successful. Try to inspect what is returned by the web server when client.UploadValues is called. Are you sure the fields are named email and password?

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
  • Yes email and password are correct field names. It returns login page. – AZee Aug 25 '13 at 22:12
  • Is there any error message like `Invalid username or password` or something like this? – Nikolay Kostov Aug 25 '13 at 22:14
  • Look at the comment below your question. Are you using cookie-aware web client? When you login a cookie is sent back to your client. The client should send that cookie in every request. – Nikolay Kostov Aug 25 '13 at 22:21
  • Yeah but seems like its not logging me in. On the other form I have webbrowser control logged in (i use that to fetch report link from anchor tag) I tried client.Headers.Add(HttpRequestHeader.Cookie, WebBrowser1.Document.Cookie) but no luck :( – AZee Aug 25 '13 at 22:28
  • if i navigate webbrowser1 to report's link it shows Download Dialog with Save option which I want to skip actually so I tried approaching WebClient to download report file – AZee Aug 25 '13 at 22:30