VB.Net/Network Remote/Web Page
Версия от 16:40, 26 мая 2010; (обсуждение)
Read Amazon web page
Imports System
Imports System.Windows.Forms
Imports System.IO
Imports System.Net
Public Class MainClass
Public Shared Sub Main()
Try
Dim theURL As New Uri("http://www.amazon.ru/exec/obidos/ASIN/1893115992")
Dim theRequest As WebRequest
theRequest = WebRequest.Create(theURL)
Dim theResponse As WebResponse
theResponse = theRequest.GetResponse
Dim aReader As New StreamReader(theResponse.GetResponseStream())
Dim theData As String
theData = aReader.ReadToEnd
Console.WriteLine("Getting Amazon rank for ISBN # 1893115992")
Console.WriteLine(theData)
Catch E As Exception
Console.WriteLine(E.StackTrace)
End Try
End Sub
End Class
Read web page tools
" Cryptography/GetWeb.vb
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Public Class GetWeb
Dim MaxContentLength As Integer = 16384 " 16k
Public Shared Sub Main(ByVal CmdArgs() As String)
If CmdArgs.Length <> 1 Then
Console.WriteLine("usage: GetWeb url")
Console.WriteLine("example: GetWeb https://www.vbex.ru")
End
End If
Dim ms As String = ""
Try
Dim req As WebRequest = WebRequest.Create(CmdArgs(0))
Dim result As WebResponse = req.GetResponse()
Dim ReceiveStream As Stream = result.GetResponseStream()
Dim enc As Encoding = System.Text.Encoding.GetEncoding("utf-8")
Dim sr As StreamReader = New StreamReader(ReceiveStream, enc)
ms = sr.ReadToEnd()
Catch x As Exception
Console.WriteLine(x.StackTrace)
Console.WriteLine("Bad URL: {0}", CmdArgs(0))
End Try
Console.WriteLine(ms)
End Sub
End Class
Snatch HTML
Public Class MainClass
Public Shared Sub Main
System.Console.WriteLine(GetPageHTML("http://www.google.ru"))
End Sub
Public Shared Function GetPageHTML(ByVal URL As String) As String
Dim objWC As New System.Net.WebClient()
Return New System.Text.UTF8Encoding().GetString(objWC.DownloadData(URL))
End Function
End Class
Snatch HTML with Timeout
Public Class MainClass
Public Shared Sub Main()
System.Console.WriteLine(GetPageHTML("http://www.google.ru", 100))
End Sub
Public Shared Function GetPageHTML(ByVal URL As String, Optional ByVal TimeoutSeconds As Integer = 10) As String
Dim objRequest As Net.WebRequest
Dim objResponse As Net.WebResponse
Dim objStreamReceive As System.IO.Stream
Dim objEncoding As System.Text.Encoding
Dim objStreamRead As System.IO.StreamReader
Try
objRequest = Net.WebRequest.Create(URL)
objRequest.Timeout = TimeoutSeconds * 1000
objResponse = objRequest.GetResponse
objStreamReceive = objResponse.GetResponseStream
objEncoding = System.Text.Encoding.GetEncoding("utf-8")
objStreamRead = New System.IO.StreamReader(objStreamReceive, objEncoding)
GetPageHTML = objStreamRead.ReadToEnd()
If Not objResponse Is Nothing Then
objResponse.Close()
End If
Catch
Return ""
End Try
End Function
End Class