Visual Basic.NET, an example of using HTTPWebRequest object

Posted by Daniel - 1,138 Views

vbnet, httpwebrequest, httpwebrequest object, httpwebrequest example, webclient, webclient objectOn the other article which you can view it here, I had described how to use the WebClient object to download resources from the internet. But you do not have enough control with the WebClient if you need to do something more. This time we’re going explore the other more flexible object related to the internet download process.

This object called HttpWebRequest. It’s located under the System.Net namespace. With this object you will have more control over the process, such as the HTTPVersion and the KeepAlive property.

I am not good in describing things, especially in English, so lets get to the point. Below is an example procedure called GetImageData() . This procedure will try to download images from the internet based on the suplied URL.

Private Sub GetImageData(ByVal strURL As String)
Dim myHttpWebRequest As HttpWebRequest
Dim ImageData As Bitmap
Dim strSavePath As String = “downloadedimg.jpg”
Try
myHttpWebRequest = CType(WebRequest.Create(strURL), HttpWebRequest)
myHttpWebRequest.KeepAlive = False
myHttpWebRequest.ProtocolVersion = HttpVersion.Version10
If Trim(strURL) <> “” Then
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
ImageData = New Bitmap(Image.FromStream(myHttpWebResponse.GetResponseStream))
ImageData.Save(strSavePath, System.Drawing.Imaging.ImageFormat.Jpeg)
ImageData.Dispose()
myHttpWebResponse.Close()
End If
Catch e As Exception
MsgBox(“ERROR: “ & e.Message)
Finally

End Try
End Sub

Demo project is included, but it’s different with the example procedure above since I use Thread in this demo project. Do not worry, its a simple one.

Download Demo Project (72 KB)

share this article

Digg del.icio.us Netscape StumbleUpon Yahoo! MyWeb reddit Furl Magnolia Newsvine Technorati SlashDot Blinklist Simpy Google
This post as PDFPosted in: Programming - January 2008


Leave a Reply


Options for your comment:





Hi, my name is Daniel Nugraha, a single male live on an island called Java, Indonesia. This is the place for me to share my interest in computer programming.

Coffee Break

Comments - Thanks Guys :)

  • Rangga Kusuma: Gan, Tengkiu buat postingan yang sangat berguna. Kebetulan ada project utk bikin sms gateway, dan converter Agan sangat berguna utk...
  • Chuck Norton: I actually went ahead & bolted over to Justin’s Get The Image plugin here: http://justintadlock.com/ar...
  • Chuck Norton: Question: is it possible to insert something like [custfieldimg=”joice1.jpg,15 0,1:1″] into the actual templates instead of...
  • Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
  • tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.