HttpWebRequestを使用して、HTTP通信をする方法を記載します。
・System.Netをインポートします。
HTTP通信
Dim html As String = ""
Dim cookie As CookieContainer = New CookieContainer()
Dim httpReq As HttpWebRequest = HttpWebRequest.Create("http://microsoft.com")
'クッキーを設定
httpReq.CookieContainer = cookie
'GET通信に設定(GET・POSTを指定)
httpReq.Method = "GET"
'ユーザー名とパスワードを設定
httpReq.Credentials = New NetworkCredential("username", "password")
'サーバー応答を受信するためのHttpWebResponseを取得
Using httpRes As HttpWebResponse = httpReq.GetResponse()
'HTTPヘッダー表示
For Each s As String In httpRes.Headers
Console.WriteLine(s)
Next
'応答データを受信するためのStreamを取得
Using Strm As System.IO.Stream = httpRes.GetResponseStream()
Using sr As New System.IO.StreamReader(Strm, System.Text.Encoding.UTF8)
'データをすべて受信
html = sr.ReadToEnd()
End Using
End Using
End Using
'取得したデータを表示する
Console.WriteLine(html)
※Credentials は、ログインとパスワードがいるサイトのみ設定します。
※CookieContainer のクッキーの設定は、クッキーを使用するサイトの時のみで、クッキーを使用しないサイトは必要ないです。
※Method は通常GETがデフォルトなので、POSTの時のみ設定が必要です。