Downloading a file via HTTP

  • Thread starter VBTricks.de.vu Webmaster
  • Start date
V

VBTricks.de.vu Webmaster

Hello everybody,

I need to download a file from the web using the HTTP-Protcoll. But
unfortunately I'm a total newbie. Found the following snippet some posts
ago:

Public Function DownloadFile(ByVal sUrl As String, _
ByRef sData As String) As Boolean
Dim myHttpWebRequest As HttpWebRequest = _
DirectCast(WebRequest.Create(sUrl), HttpWebRequest)
myHttpWebRequest.Method = "POST"
myhttpwebrequest.Proxy.Credentials

' Create a new string object to POST data to the Url.
Dim inputData As String = "cmd=" & HttpUtility.UrlEncode("login")

Dim postData As String = inputData
Dim encoding As New ASCIIEncoding
Dim byte1 As Byte() = encoding.GetBytes(postData)

' Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"

'Do I need to set cotnet type as below if transfer data file?
myHttpWebRequest.ContentType = "multipart/form-data encode"


' Set the content length of the string being posted.
myHttpWebRequest.ContentLength = postData.Length

Dim newStream As Stream = myHttpWebRequest.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
newStream.Close()
' sData = ?
End Function

But how do I transform it to a GET-request (as I do not need to send any
data to the server)? Second, how do I get the data returned by
GetRequestStream into my String sData?


Need your help,

Stefan

--
___________________________________www.VBTricks.de.vu
the free resource for Visual Basic, Gambas and Pascal
components, tips & complete projects

www: http://www.VBTricks.de.vu
mail: vbtricks <at> gmx <dot> net
_____________________________________________________
 
H

Herfried K. Wagner [MVP]

VBTricks.de.vu Webmaster said:
I need to download a file from the web using the HTTP-Protcoll.

\\\
Imports System.IO
Imports System.Net
..
..
..
Public Function LoadTextFile(ByVal Url As String) As String
Dim wrq As WebRequest = WebRequest.Create(Url)
Dim wrp As HttpWebResponse = _
DirectCast(wrq.GetResponse(), HttpWebResponse)
Dim sr As New StreamReader(wrp.GetResponseStream)
Dim Text As String = sr.ReadToEnd()
sr.Close()
wrp.Close()
Return Text
End Function
///
 
V

VBTricks.de.vu Webmaster

Thx, but oops I forgot, I need an asynchronous version giving me the
possibilty to show the current progress and (might be more important) to
cancel.

Is this also possible with the .NET framework or do I have to implement
it myself?

Thanks in advance,

Stefan

--
___________________________________www.VBTricks.de.vu
the free resource for Visual Basic, Gambas and Pascal
components, tips & complete projects

www: http://www.VBTricks.de.vu
mail: vbtricks <at> gmx <dot> net
_____________________________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top