VB.Net Tutorial/Socket Network/Socket Web Client

Материал из VB Эксперт
Версия от 12:55, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Read a web page with Socket

Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Text
Imports System.Text.Encoding
Public Class Tester
    Public Shared Sub Main
        Dim serverIP As IPAddress = Dns.Resolve("www.vbex.ru").AddressList(0)
        Dim Port As String = "80"
        Dim serverhost As New IPEndPoint(serverIP, Int32.Parse(Port))
    
        Dim clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    
        Try
          clientSocket.Connect(serverhost)
    
          If clientSocket.Connected = False Then
            Console.WriteLine("Connect Error.")
            Exit Sub
          End If
    
          Dim httpReq As String = "GET / HTTP/1.0" & ControlChars.CrLf & ControlChars.CrLf
    
          clientSocket.Send(ASCII.GetBytes(httpReq))
    
          Dim buffer(1024) As Byte
          Dim byteCount As Int16 = clientSocket.Receive(buffer, buffer.Length, 0)
    
          Console.WriteLine(ASCII.GetString(buffer, 0, byteCount))
    
          Do While byteCount > 0
            byteCount = clientSocket.Receive(buffer, buffer.Length, 0)
            Console.WriteLine(ASCII.GetString(buffer, 0, byteCount))
          Loop
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    
    End Sub
End Class

Read from web

Imports System.Net.Sockets
Imports System.Net
Imports System.IO
Imports System.Text
Public Class Tester
    Public Shared Sub Main
        Dim httpReq As System.Net.HttpWebRequest
        Dim httpResp As System.Net.HttpWebResponse
    
        Dim respStream As Stream
        Dim respStreamReader As StreamReader
        Dim strBuff As String = Nothing
        Dim cbuffer(256) As Char
        Dim byteRead As Integer = 0
        Dim index As Integer = 0
    
        Try
          Dim httpURL As New System.Uri("http://www.vbex.ru")
    
          httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
          httpResp = CType(httpReq.GetResponse(), HttpWebResponse)
    
          respStream = httpResp.GetResponseStream()
          respStreamReader = New StreamReader(respStream, Encoding.UTF8)
    
          byteRead = respStreamReader.Read(cbuffer, 0, 256)
    
          While (byteRead <> 0)
            Dim strResp As New [String](cbuffer, 0, byteRead)
            Console.WriteLine(strResp)
            byteRead = respStreamReader.Read(cbuffer, 0, 256)
          End While
          respStream.Close()
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    End Sub
End Class
<HTML>
<HEAD>
     Java examples (example source code) Organized by topic </title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">    <meta http-equiv="content
-style-type" content="text/css"/>
    <meta name="author" content=
Demo Source and Support Ltd."/>
    <meta name="copyright" content="2006 Demo Source and Support Ltd."/>
    <meta name="description" CONTENT=" Java examples (example source code) Organized by topic " />
    <meta name="keywords" CONTENT=" Java examples (
example source code) Organized by topic "/>
<SCRIPT language=javascript1.2>
<!--  document.write("<style type="text/css">.script12hide { display: none; }</style>");  -->
</SCRIPT>
<STYLE type=text/css>
.BackgroundTable {BACKGROUND-COLOR: #fff4c2; BORDER-RI
GHT:#ffcc33 1px solid; BORDER-LEFT:#ffcc33 1px solid;BORDER-TOP:#ffcc33 1px solid; BORDER-BOTTOM:#ff
cc33 1px solid}