简体   繁体   中英

Download a file From https using VB.net

Could anyone help me please ?

I need to download a file from web ie https:\\www.xxx.com\\ using vb.net and save it to C drive of system.

Below is the code :

Dim URI As String = ftpHost & ftpFile
Dim oRequest As System.Net.HttpWebRequest = CType(HttpWebRequest.Create(URI), HttpWebRequest)
oRequest.Credentials = New System.Net.NetworkCredential(userName, pwd)        
Using oResponse As System.Net.WebResponse = CType(oRequest.GetResponse, System.Net.WebResponse)
    Using responseStream As IO.Stream = oResponse.GetResponseStream
        Using fs As New IO.FileStream(localFile, FileMode.Create, FileAccess.Write)
            Dim buffer(2047) As Byte
            Dim read As Integer
            Do
            read = responseStream.Read(buffer, 0,buffer.Length)
                fs.Write(buffer, 0, read)
            Loop Until read = 0
            responseStream.Close()
            fs.Flush()
            fs.Close()
        End Using
        responseStream.Close()
    End Using
    oResponse.Close()
End Using

But this is not reading anything.

Thanks in advance.

I ran your code downloading the latest jQuery library from https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js and everything worked fine. I used a dummy username/password of 'name'/'pwd'. The only thing I can think of is that your credentials aren't valid. If you change your code to download the jQuery file I mentioned above, does it work? If it does, I'd take a look at the creds you're passing and also how you're processing them on the server side.

Hope this helps.

---modified code---

    Dim URI As String = "https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"
    Dim oRequest As System.Net.HttpWebRequest = CType(HttpWebRequest.Create(URI), HttpWebRequest)
    oRequest.Credentials = New System.Net.NetworkCredential("name", "pwd")
    Using oResponse As System.Net.WebResponse = CType(oRequest.GetResponse, System.Net.WebResponse)
        Using responseStream As IO.Stream = oResponse.GetResponseStream
            Using fs As New IO.FileStream("c:\temp\jquery-1.4.2.js", FileMode.Create, FileAccess.Write)
                Dim buffer(2047) As Byte
                Dim read As Integer
                Do
                    read = responseStream.Read(buffer, 0, buffer.Length)
                    fs.Write(buffer, 0, read)
                Loop Until read = 0
                responseStream.Close()
                fs.Flush()
                fs.Close()
            End Using
            responseStream.Close()
        End Using
        oResponse.Close()
    End Using

Does your target site https://foo.com/bar.txt present a certificate for foo.com or another site?

If it doesn't have a cert for foo.com , this might be part of the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM