用来获取网页的类(转!vb.net参考)

Imports  System.Net
Imports  System.IO

Public   Class  HttpDriverClass HttpDriver

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

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

    
Public   Function  GetPage() Function  GetPage( ByVal  url  As   String ByRef  postPara  As   String ByRef  cookies  As  CookieCollection,  ByVal  hasCookie  As   Boolean Optional   ByRef  encType  As   String   =   " GB2312 " Optional   ByRef  refer  As   String   =   "" As   String
        
If  (url.StartsWith( " http:// " =   False Then
            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() Function  GetPage( ByVal  url  As   String ByRef  postPara  As  System.Collections.Hashtable,  ByRef  cookies  As  CookieCollection,  ByVal  hasCookie  As   Boolean Optional   ByRef  encType  As   String   =   " GB2312 " As   String
        
Return  GetPage(url, ColToStr(postPara), cookies,  True , encType)
    
End Function

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

    
' 该函数用于转换表单项集合为字符串
     Public   Shared   Function  ColToStr() 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( 0 str .Length  -   1 )
        
Return   str
    
End Function

End Class


如果需要支持cookie,并支持refer,可以通过下面这个类来使用上面的httpdriver。


' 该类用于访问含有cookie的页面
Imports  System.IO

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

    
Public   Function  GetPage() Function  GetPage( ByVal  url  As   String Optional   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() Function  GetPage( ByVal  url  As   String ByRef  postPara  As  Hashtable,  Optional   ByRef  encType  As   String   =   " GB2312 " As   String
        
Return  GetPage(url, hd.ColToStr(postPara), encType)
    
End Function

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

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


轻量的get网页的函数
Public   Function  GetPage() Function  GetPage( ByVal  url  As   String As   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)

Post方法:
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)
 
<script type="text/javascript"> google_ad_client = "pub-8527320164371593"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; //2006-11-09: asp.net google_ad_channel = "8786117784"; google_language = 'en'; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值