Could anyone help me please ?
I need to download a file from web i.e 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, FileMod开发者_JS百科e.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
take a look at this http://www.devhood.com/messages/message_view-2.aspx?thread_id=39924
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.
精彩评论