简体   繁体   English

使用VB.net从https下载文件

[英]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. 我需要使用vb.net从网络(即https://www.xxx.com \\)下载文件,并将其保存到系统的C驱动器中。

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. 我运行了您的代码,从https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js下载了最新的jQuery库,一切正常。 I used a dummy username/password of 'name'/'pwd'. 我使用了'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? 如果您更改代码以下载上面提到的jQuery文件,那么它行得通吗? 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? 您的目标站点https://foo.com/bar.txt是否提供foo.com或其他站点的证书?

If it doesn't have a cert for foo.com , this might be part of the problem. 如果它没有foo.com的证书,则可能是问题的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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