Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to download via WebClient now.

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
0
0
US
A site we use to get data files from has recently change how their security works and my window program no longer works. Before I was able to use the IE web browser plug-in to login and navigate to the download page. I would collect all of the links and then download the files using the WebClient and DownloadFileAsync Method. The problem is that with the security change the file doesn't download it is instead redirected to the login page. I've tried multiple ways to send the username/password information, but nothing happens.

When searching the net I've found a few people have had similar problems, but no one generally answers. The few times they did they simply said to use HttpWebRequest. The problem with this is the two are designed differently so I would either have to make a whole new class base around the HttpWebRequest or totally remake my program because of the differences in the two.

I've tried setting the Credentials as well as trying to login using the UploadValues method when the WebClient hits the page and neither works. Is there any way to get the current way to work with the WebClient? I really don't see any benefit to using the HttpWebRequest for what I'm doing nor does anyone give any explanation why some one should use the other way when they suggest it. Oh, in case it makes a difference the login is a PHP page, but the download links are done as ASP pages.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
This is the closest I've come to possibly something right. It at least doesn't respond with the login page. The problem is that it doesn't respond with any page.

Code:
 Dim request As HttpWebRequest = CType(WebRequest.Create(Link), HttpWebRequest)
                request.CookieContainer = New CookieContainer
                request.CookieContainer.Add(cAddCookie)
                request.ContentType = "application/x-[URL unfurl="true"]www-form-urlencoded"[/URL] '"text/html; charset=utf-8"
                request.Method = "POST"
                request.ContentLength = Link.Length
                request.AllowAutoRedirect = True
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"
                request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash," _
                                     & "application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"

                Dim strcook As String = request.CookieContainer.GetCookieHeader(New Uri(Link))

                Dim requestStream As IO.Stream = request.GetRequestStream()
                Dim postBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(Link)
                requestStream.Write(postBytes, 0, postBytes.Length)


                Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
                Dim streamResponse As IO.Stream = response.GetResponseStream()
                Dim streamRead As New IO.StreamReader(streamResponse)
                Dim responseString As String = streamRead.ReadToEnd()

                response = CType(request.GetResponse(), HttpWebResponse)
                streamResponse = response.GetResponseStream()
                streamRead = New IO.StreamReader(streamResponse)
                responseString = streamRead.ReadToEnd()

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Well that was stupid I didn't even notice I was calling the responce twice and that is why it is blank. So I've actually made no progress at all. :(

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top