VB.Net Tutorial/Socket Network/HttpWebRequest

Материал из VB Эксперт
Перейти к: навигация, поиск

Connection, ConnectionGroupName, ContentLength, ContentType

Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
        Dim httpReq As System.Net.HttpWebRequest
    
        Try
          Dim httpURL As New System.Uri("http://www.vbex.ru/index.htm?key=value")
    
          httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
    
          If httpReq.Connection <> Nothing Then
            Console.WriteLine("Connection: " & httpReq.Connection.ToString)
          End If
          If httpReq.ConnectionGroupName <> Nothing Then
            Console.WriteLine("ConnectionGroupName: " & httpReq.ConnectionGroupName.ToString)
          End If
          If httpReq.ContentLength <> -1 Then
            Console.WriteLine("ContentLength: " & httpReq.ContentLength.ToString)
          End If
          If httpReq.ContentType <> Nothing Then
            Console.WriteLine("ContentType: " & httpReq.ContentType.ToString)
          End If
    
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    End Sub
End Class

Create HttpWebRequest

Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
        Dim httpReq As System.Net.HttpWebRequest
    
        Try
          Dim httpURL As New System.Uri("http://www.vbex.ru/index.htm?key=value")
    
          httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
    
          Console.WriteLine("Address: " & httpReq.Address.ToString)
          Console.WriteLine("AllowAutoRedirect: " & httpReq.AllowAutoRedirect.ToString)
          Console.WriteLine("AllowWriteStreamBuffering: " & httpReq.AllowWriteStreamBuffering.ToString)
          Console.WriteLine("ClientCertificates: " & httpReq.ClientCertificates.ToString)
    
    
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    End Sub
End Class
Address: http://www.vbex.ru/index.htm?key=value
AllowAutoRedirect: True
AllowWriteStreamBuffering: True
ClientCertificates: System.Security.Cryptography.X509Certificates.X509CertificateCollection

HaveResponse, IfModifiedSince, KeepAlive, MaximumAutomaticRedirections

Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
        Dim httpReq As System.Net.HttpWebRequest
    
        Try
          Dim httpURL As New System.Uri("http://www.vbex.ru/index.htm?key=value")
    
          httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
    
    
          Console.WriteLine("HaveResponse: " & httpReq.HaveResponse.ToString)
          Console.WriteLine("IfModifiedSince: " & httpReq.IfModifiedSince.ToString)
          Console.WriteLine("KeepAlive: " & httpReq.KeepAlive)
          Console.WriteLine("MaximumAutomaticRedirections: " & httpReq.MaximumAutomaticRedirections)
    
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    End Sub
End Class
HaveResponse: False
IfModifiedSince: 11/05/2007 9:42:43 PM
KeepAlive: True
MaximumAutomaticRedirections: 50

HttpWebResponse and its properties

Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
        Dim httpReq As System.Net.HttpWebRequest
        Dim httpResp As System.Net.HttpWebResponse
    
        Try
          Dim httpURL As New System.Uri("http://www.vbex.ru")
    
          httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
          httpResp = CType(httpReq.GetResponse(), HttpWebResponse)
    
          Console.WriteLine("CharacterSet: " & httpResp.CharacterSet.ToString)
          Console.WriteLine("ContentEncoding: " & httpResp.ContentEncoding.ToString)
          Console.WriteLine("ContentLength: " & httpResp.ContentLength.ToString)
          Console.WriteLine("ContentType: " & httpResp.ContentType.ToString)
          Console.WriteLine("LastModified: " & httpResp.LastModified.ToString)
          Console.WriteLine("Method: " & httpResp.Method.ToString)
          Console.WriteLine("ProtocolVersion: " & httpResp.ProtocolVersion.ToString)
          Console.WriteLine("ResponseUri: " & httpResp.ResponseUri.ToString)
          Console.WriteLine("Server: " & httpResp.Server.ToString)
          Console.WriteLine("StatusCode: " & httpResp.StatusCode.ToString)
          Console.WriteLine("StatusDescription: " & httpResp.StatusDescription.ToString)
    
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    End Sub
End Class
CharacterSet: ISO-8859-1
ContentEncoding:
ContentLength: 343984
ContentType: text/html
LastModified: 17/04/2007 10:07:49 PM
Method: GET
ProtocolVersion: 1.1
ResponseUri: http://www.vbex.ru/
Server: Apache/2.0.54 (Fedora)
StatusCode: OK
StatusDescription: OK

MediaType

Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
        Dim httpReq As System.Net.HttpWebRequest
    
        Try
          Dim httpURL As New System.Uri("http://www.vbex.ru/index.htm?key=value")
    
          httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
    
          If httpReq.MediaType <> Nothing Then
            Console.WriteLine("MediaType: " & httpReq.MediaType.ToString)
          End If
    
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    End Sub
End Class

Method, PreAuthenticate, ProtocolVersion

Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
        Dim httpReq As System.Net.HttpWebRequest
    
        Try
          Dim httpURL As New System.Uri("http://www.vbex.ru/index.htm?key=value")
    
          httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
    
          Console.WriteLine("Method: " & httpReq.Method.ToString)
          Console.WriteLine("PreAuthenticate: " & httpReq.PreAuthenticate)
          Console.WriteLine("ProtocolVersion: " & httpReq.ProtocolVersion.ToString)
    
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    End Sub
End Class
Method: GET
PreAuthenticate: False
ProtocolVersion: 1.1

Referer, RequestUri, RequestUri

Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
        Dim httpReq As System.Net.HttpWebRequest
    
        Try
          Dim httpURL As New System.Uri("http://www.vbex.ru/index.htm?key=value")
    
          httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
    
          If httpReq.Referer <> Nothing Then
            Console.WriteLine("Referer: " & httpReq.Referer.ToString)
          End If
    
          Console.WriteLine("RequestUri: " & httpReq.RequestUri.ToString)
    
          If (httpReq.RequestUri.ToString() <> httpReq.Address.ToString()) Then
            Console.WriteLine("URL has been changed.")
          End If
    
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    End Sub
End Class
RequestUri: http://www.vbex.ru/index.htm?key=value

TransferEncoding, UserAgent

Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
        Dim httpReq As System.Net.HttpWebRequest
    
        Try
          Dim httpURL As New System.Uri("http://www.vbex.ru/index.htm?key=value")
    
          httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
    
          If httpReq.TransferEncoding <> Nothing Then
            Console.WriteLine("TransferEncoding: " & httpReq.TransferEncoding.ToString)
          End If
    
          If httpReq.UserAgent <> Nothing Then
            Console.WriteLine("UserAgent: " & httpReq.UserAgent.ToString)
          End If
    
        Catch ex As Exception
          Console.WriteLine(ex.StackTrace.ToString())
        End Try
    End Sub
End Class