7

Trying to download file in code.

Current code:

 Dim uri As New UriBuilder uri.UserName = "xxx" uri.Password = "xxx" uri.Host = "xxx" uri.Path = "xxx.aspx?q=65" Dim request As HttpWebRequest = DirectCast(WebRequest.Create(uri.Uri), HttpWebRequest) request.AllowAutoRedirect = True request = DirectCast(WebRequest.Create(DownloadUrlIn), HttpWebRequest) request.Timeout = 10000 'request.AllowWriteStreamBuffering = True Dim response As HttpWebResponse = Nothing response = DirectCast(request.GetResponse(), HttpWebResponse) Dim s As Stream = response.GetResponseStream() 'Write to disk Dim fs As New FileStream("c:\xxx.pdf", FileMode.Create) Dim read As Byte() = New Byte(255) {} Dim count As Integer = s.Read(read, 0, read.Length) While count > 0 fs.Write(read, 0, count) count = s.Read(read, 0, read.Length) End While 'Close everything fs.Close() s.Close() response.Close() 

Running this code and checking the response.ResponseUri indicates im being redirected back to the login page and not to the pdf file.

For some reason its not authorising access what could I be missing as Im sending the user name and password in the uri? Thanks for your help

2
  • Does the page actually allow authorization via the url? Or do you need to authorize via a web interface that sets a cookie? Im guessing the later, in which case you will need to perform 2 requests, 1 to login and store the cookie, the second to request the pdf (using the cookie) Commented Nov 24, 2012 at 8:30
  • The code gave me a good start to perform my downloading. Thanks mate Commented Feb 23, 2013 at 22:48

3 Answers 3

4

You don't need all of that code to download a file from the net just use the WebClient class and its DownloadFile method

Sign up to request clarification or add additional context in comments.

1 Comment

Have tried this but with same problem that its redirecting to the login page.
0

you should check and see if the site requires cookies (most do), i'd use a packet analyzer and run your code and see exactly what the server is returning. use fiddler or http analyzer to log packets

1 Comment

Chrome developer tools or firefox with firebug should be enough to determine the existance of cookies and the actual request and response headers. But there are more sophisticated methods of scripting detection on some web applications.
0

With UWP, this has become a more pertinent question as UWP does not have a WebClient. The correct answer to this question is if you are being re-directed to the login page, then there must be an issue with your credentials OR the setting (or lack of) header for the HttpWebRequest.

According to Microsoft, the request for downloading is sent with the call to GetResponse() on the HttpWebRequest, therefore the downloaded file SHOULD be in the stream in the response (returned by the GetResponse() call mentioned above).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.