VB.Net/Network Remote/Web Request
Содержание
Download Data from a URI
Imports System.IO
Imports System.Net
Imports System.Text
public class MainClass
Shared Sub Main()
Dim uri As New Uri("http://www.vbex.ru/index.htm")
Dim request As WebRequest = WebRequest.Create(uri)
Dim response As WebResponse = request.GetResponse()
Dim stream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(stream)
Dim data As String = reader.ReadToEnd()
reader.Close()
stream.Close()
Console.WriteLine(data)
End Sub
End Class
Get response and response stream from WebRequest
Imports System.Net
Imports System.IO
Public Class MainClass
Public Shared Sub Main()
Dim PageRequest As HttpWebRequest = WebRequest.Create("www.google.ru")
Dim PageResponse As WebResponse = PageRequest.GetResponse()
Dim PageReader As New StreamReader(PageResponse.GetResponseStream())
Dim PageString As String = PageReader.ReadToEnd()
PageReader.Close()
System.Console.WriteLine(PageString)
End Sub
End Class
Google Client
Imports System.IO
Imports System.Net
Imports System.Web
Imports System.Collections.Generic
Public Class GoogleClient
Public Function Define(ByVal word As String) As String()
Dim req As HttpWebRequest = Nothing
Dim resp As HttpWebResponse
Dim query As String
Dim result As New List(Of String)
query = "http://www.google.ru/search?q=define%3A" & HttpUtility.UrlEncode(word)
Try
req = DirectCast(WebRequest.Create(query), HttpWebRequest)
With req
.Method = "GET"
resp = req.GetResponse
If resp.StatusCode = HttpStatusCode.OK Then
ParseResponse(resp.GetResponseStream, result)
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 result.ToArray()
End Function
Private Sub ParseResponse(ByVal input As System.IO.Stream, ByRef output As List(Of String))
Dim reader As New StreamReader(input)
Dim work As String = reader.ReadToEnd
System.Console.WriteLine(work)
End Sub
End Class
Is Connection Available
Public Class MainClass
Public Shared Sub Main()
System.Console.WriteLine(IsConnectionAvailable())
End Sub
Public Shared Function IsConnectionAvailable() As Boolean
Dim objUrl As New System.Uri("http://www.yoursite.ru/")
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objResp As System.Net.WebResponse
Try
objResp = objWebReq.GetResponse
objResp.Close()
objWebReq = Nothing
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
Web Request and Response: type and content type, length
Imports System.IO
Imports System.Net
Imports System.Text
public class MainClass
Shared Sub Main()
Dim uri As New Uri("http://www.vbex.ru/index.htm")
Dim request As WebRequest = WebRequest.Create(uri)
Dim response As WebResponse = request.GetResponse()
Console.WriteLine("Request type: " & request.GetType().ToString() )
Console.WriteLine("Response type: " & response.GetType().ToString() )
Console.WriteLine("Content length: " & response.ContentLength & " bytes" )
Console.WriteLine("Content type: " & response.ContentType )
End Sub
End Class