Networking VB.NET (Web Request, Web Respons, URI Class)

Belajar Vb.Net Pemula - Pelajaran vb.net tentang Networking class yang bisa digunakan para depelover aplikasi vb.net diantaranya Class WebRequest, HttpWebRequest, request.GetType(), response.ContentLength, response.ContentType, WebResponse, Download data dari URI Class dalam vb.net, baca Penggunaan HttpWebRequest Class, HttpWebResponse Vb.Net, Tutorial kali ini merupakan kelanjutan dari postingan sebelumnya yang akan membahas secara detail tentang  Penggunaan Class WebRequest dan WebResponse dalam mendownload Data dari sebuah URI, Menampilkan Respon dari sebuah class webRequest, Searching dengan Google, mengetahui apakah koneksi website ada atau tidak, dan yang terakhir tentang Web Request dan Response: type serta content type  length dalam VB.Net. simak terus tutorialnya ya.

Buatlah sebuah project aplikasi console untuk memudahkan kamu untuk belajar, ikuti step - by - step tutorial Networking VB.NET (Web Request, Web Respons, URI Class) ini :
Networking VB.NET (Web Request, Web Respons, URI Class)

Download Data dari class URI

Imports System.IO
Imports System.Net
Module Module1
Sub Main()
Dim Geturi As Uri
Dim Getrequest As WebRequest
Dim Getresponse As WebResponse
Dim Getstream As Stream
Dim Getreader As StreamReader
Dim Getdata As String
Geturi = New Uri("https://www.facebook.com/SectorsCode")

Getrequest = WebRequest.Create(Geturi)
Getresponse = Getrequest.GetResponse()
Getstream = Getresponse.GetResponseStream()
Getreader = New StreamReader(Getstream)
Getdata = Getreader.ReadToEnd()
Getreader.Close()
Getstream.Close()
Console.WriteLine(Getdata)
Console.WriteLine("=================================")
Console.WriteLine("Get data Success!")
Console.ReadKey()
End Sub

End Module

Mendapatkan Response dari WebRequest

Imports System.IO
Imports System.Net
Module Module1
Sub Main()
Dim CreateRequest As HttpWebRequest
Dim GetResponse As WebResponse
Dim GetReader As StreamReader
Dim GetString As String
CreateRequest = WebRequest.Create("https://www.facebook.com/SectorsCode")
GetResponse = CreateRequest.GetResponse()
GetReader = New StreamReader(GetResponse.GetResponseStream())
GetString = GetReader.ReadToEnd()
GetReader.Close()
Console.WriteLine(GetString)
Console.ReadKey()
End Sub

End Module

Baca dari Web Client

Imports System.IO
Imports System.Net
Public Class Class1
Public Property HttpUtility As Object
Public Function Define(ByVal word As String) _
As String()
Dim Getrequest As HttpWebRequest = Nothing
Dim Getrespons As HttpWebResponse
Dim Createquery As String
Dim Getresult As New List(Of String)
Createquery = "https://www.google.com/search?q=scqq" _
& HttpUtility.UrlEncode(word)
Try
Getrequest = DirectCast(WebRequest.Create(Createquery),
HttpWebRequest)
With Getrequest
.Method = "GET"
Getrespons = Getrequest.GetResponse
If Getrespons.StatusCode = HttpStatusCode.OK Then
ParseResponse(Getrespons.GetResponseStream,
Getresult)
Else
System.Console.WriteLine("Error " _
+ "calling definition service")
End If
End With
Catch ex As Exception
System.Console.WriteLine("Error getting definitions")
End Try
Return Getresult.ToArray()
End Function
Private Sub ParseResponse(ByVal input As _
System.IO.Stream, ByRef output As List(Of String))
Dim Getreader As New StreamReader(input)
Dim Dowork As String = Getreader.ReadToEnd
System.Console.WriteLine(Dowork)
End Sub
End Class

Cek apakah Koneksi Available atau tidak

Imports System.Net
Module Module1
Public Function IsConnectionAvailable() As Boolean
Dim CreateUrl As Uri
Dim Createreq As WebRequest
Dim Getrespons As WebResponse
CreateUrl = New Uri("http://scqq.blogspot.com/")
Createreq = WebRequest.Create(CreateUrl)
Try
Getrespons = Createreq.GetResponse
Getrespons.Close()
Createreq = Nothing
Return True
Catch ex As Exception
Return False
End Try
End Function
Sub Main()
Console.WriteLine("Cek koneksi ke 'http://scqq.blogspot.com'")
Console.WriteLine(IsConnectionAvailable())
Console.ReadKey()
End Sub

End Module

Baca Type Request dan Response

Imports System.Net
Module Module1
Sub Main()
Dim Createuri As Uri
Dim Createrequest As WebRequest
Dim Getresponse As WebResponse
Createuri = New Uri("http://scqq.blogspot.com/")
Createrequest = WebRequest.Create(Createuri)
Getresponse = Createrequest.GetResponse()
Console.WriteLine("Request type: " & Createrequest.GetType().ToString())
Console.WriteLine("Response type: " & Getresponse.GetType().ToString())
Console.WriteLine("Content length: " & Getresponse.ContentLength & " bytes")
Console.WriteLine("Content type: " & Getresponse.ContentType)
Console.ReadKey()
End Sub

End Module