使用VB.net从https下载文件

时间:2010-07-21 16:24:39

标签: .net vb.net

有人可以帮我吗?

我需要从网上下载一个文件,即https:\ www.xxx.com \使用vb.net并将其保存到系统的C盘。

以下是代码:

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

但这不是读任何东西。

提前致谢。

3 个答案:

答案 0 :(得分:1)

我运行你的代码从https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js下载最新的jQuery库,一切正常。我使用了'name'/'pwd'的虚拟用户名/密码。我唯一能想到的是你的凭证无效。如果您更改代码以下载我上面提到的jQuery文件,它是否有效?如果是这样的话,我会看看你传递的信息,以及你如何在服务器端处理它们。

希望这有帮助。

---修改代码---

    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

答案 1 :(得分:0)

答案 2 :(得分:0)

您的目标网站https://foo.com/bar.txt是否为foo.com或其他网站提供了证书?

如果它没有foo.com的证书,这可能是问题的一部分。