用來獲取網頁的類

最後更新時間:2005年9月7日
更新了HttpDriver類

這個經過測試,使用上比較穩定,因爲考慮到統一的錯誤處理,類裏面沒有catch任何錯誤,所有網絡錯誤都在使用的時候捕獲,以便決定重試或終止。支持get和post,支持自定義編碼,支持cookie,但不支持上傳文件。
Imports System.Net
Imports System.IO

Public Class HttpDriver

    
Public Function GetPage(ByVal url As StringOptional ByRef postPara As String = ""Optional ByRef encType As String = "GB2312"As String
        
Return GetPage(url, postPara, NothingFalse, encType)
    
End Function


    
Public Function GetPage(ByVal url As StringByRef postPara As System.Collections.Hashtable, Optional ByRef encType As String = "GB2312"As String
        
Return GetPage(url, ColToStr(postPara), encType)
    
End Function


    
Public Function GetPage(ByVal url As StringByRef postPara As StringByRef cookies As CookieCollection, ByVal hasCookie As BooleanOptional ByRef encType As String = "GB2312"Optional ByRef refer As String = ""As String
        
If (url.StartsWith("http://"= FalseThen
            url 
= "http://" & url
        
End If
        
Dim hRqst As HttpWebRequest = HttpWebRequest.Create(url)
        
If (hasCookie = True AndAlso (Not cookies Is Nothing)) Then
            hRqst.CookieContainer 
= New CookieContainer
            hRqst.CookieContainer.Add(cookies)
        
End If

        hRqst.ContentType 
= "application/x-www-form-urlencoded"
        hRqst.Headers.Add("Accept-Language""zh-cn")
        
Dim streamData As Stream
        
Dim bt() As Byte
        
If (postPara = ""Then
            hRqst.Method 
= "GET"
        Else
            hRqst.Method 
= "POST"
            hRqst.AllowWriteStreamBuffering = True
            bt 
= System.Text.Encoding.ASCII.GetBytes(postPara)
            hRqst.ContentLength 
= bt.Length
            hRqst.UserAgent 
= "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
            hRqst.Referer = refer
            hRqst.KeepAlive 
= False
            hRqst.Timeout 
= 20000
            streamData 
= hRqst.GetRequestStream()
            streamData.
Write(bt, 0, bt.Length)
            streamData.Close()
        
End If
        
Dim hRsp As HttpWebResponse
        hRsp 
= hRqst.GetResponse()
        streamData 
= hRsp.GetResponseStream()
        
If (hasCookie = True AndAlso (Not hRsp.Cookies Is Nothing)) Then
            cookies.Add(hRsp.Cookies)
        End If

        
If (encType = ""Then
            encType 
= "GB2312"
        End If
        
Dim readStream As New IO.StreamReader(streamData, System.Text.Encoding.GetEncoding(encType))
        GetPage 
= readStream.ReadToEnd()
        streamData.Close()
    
End Function


    
Public Function GetPage(ByVal url As StringByRef postPara As System.Collections.Hashtable, ByRef cookies As CookieCollection, ByVal hasCookie As BooleanOptional ByRef encType As String = "GB2312"As String
        
Return GetPage(url, ColToStr(postPara), cookies, True, encType)
    
End Function


    
Public Function GetPage(ByVal url As StringByRef cookies As CookieCollection, ByVal hasCookie As BooleanOptional ByRef encType As String = "GB2312"As String
        
Return GetPage(url, "", cookies, True, encType)
    
End Function


    
'該函數用於轉換表單項集合爲字符串
    Public Shared Function ColToStr(ByRef ht As System.Collections.Hashtable, Optional ByRef encType As String = "GB2312"As String
        
Dim str As String
        
Dim para As DictionaryEntry
        
For Each para In ht
            
str &= System.Web.HttpUtility.UrlEncode(CType(para.Key, String), Text.Encoding.GetEncoding(encType))
            
str &= "="
            str &= System.Web.HttpUtility.UrlEncode(CType(para.Value, String), Text.Encoding.GetEncoding(encType))
            
str &= "&"
        Next
        
str = str.Substring(0str.Length - 1)
        
Return str
    
End Function


End Class



如果需要支持cookie,並支持refer,可以通過下面這個類來使用上面的httpdriver。
'該類用於訪問含有cookie的頁面
Imports System.IO

Public Class UserAgent
    
Private m_cookies As New System.Net.CookieCollection
    
Private refer As String
    
Private hd As New HttpDriver

    
Public Function GetPage(ByVal url As StringOptional ByRef postPara As String = ""Optional ByRef encType As String = "GB2312"As String
        GetPage 
= hd.GetPage(url, postPara, m_cookies, True, encType, refer)
        refer 
= url
    
End Function


    
Public Function GetPage(ByVal url As StringByRef postPara As Hashtable, Optional ByRef encType As String = "GB2312"As String
        
Return GetPage(url, hd.ColToStr(postPara), encType)
    
End Function


    
Public Function SetCookie(ByVal cookies As System.Net.CookieCollection)
        m_cookies 
= cookies
    
End Function


    
Public Function GetCookie() As System.Net.CookieCollection
        
Return m_cookies
    
End Function

End Class



輕量的get網頁的函數
Public Function GetPage(ByVal url As StringAs String
    
Dim hRqst As HttpWebRequest = HttpWebRequest.Create(url)

    hRqst.ContentType 
= "application/x-www-form-urlencoded"
    hRqst.Method = "GET"
    Dim streamData As Stream

    
Dim hRsp As HttpWebResponse = hRqst.GetResponse()
    streamData 
= hRsp.GetResponseStream()

    
Dim readStream As New IO.StreamReader(streamData, System.Text.Encoding.GetEncoding("GB2312"))
    GetPage 
= readStream.ReadToEnd()
    streamData.Close()
End Function


Get方法:
Dim ua as New UserAgent
Dim content as String
Dim url as String
url 
= "www.sina.com.cn"
content = ua.GetPage(url)
   url = "sohu.com"
content = ua.GetPage(url)

Post方法(頁面和參數名字都是示例)。UserAgent的好處是可以透明的保存cookie,用以下的方法就可以實現登錄後保存登錄信息了。
Dim ua as New UserAgent
Dim content as String
Dim url as String
Dim ht as New HashTable

url 
= "mail.sina.com.cn"
ht.Add("username""用戶名")
ht.Add(
"password""密碼")
content 
= ua.GetPage(url, ht)
   url = "mail.sina.com.cn/default.htm"
   content = ua.GetPage(url)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章