用來獲取網頁的類(轉!vb.net參考)
阿新 • • 發佈:2019-02-07
Imports System.Net
Imports System.IO
PublicClass HttpDriverClass HttpDriver
PublicFunction GetPage()Function GetPage(ByVal url AsString, OptionalByRef postPara AsString="", OptionalByRef encType AsString="GB2312") AsStringReturn GetPage(url, postPara, Nothing, False, encType)
End FunctionPublicFunction GetPage()Function GetPage(ByVal url AsString, ByRef postPara As System.Collections.Hashtable, OptionalByRef encType AsString="GB2312") AsStringReturn GetPage(url, ColToStr(postPara), encType)
End FunctionPublicFunction GetPage()Function GetPage(ByVal url AsString, ByRef postPara AsString, ByRef cookies As CookieCollection, ByVal hasCookie AsBoolean, OptionalByRef encType AsString="GB2312", OptionalByRef refer AsString="") AsStringIf (url.StartsWith("http://") =False) Then
url ="http://"& url
EndIfDim hRqst As HttpWebRequest = HttpWebRequest.Create(url)
If (hasCookie =TrueAndAlso (Not cookies IsNothing)) Then
hRqst.CookieContainer =New CookieContainer
hRqst.CookieContainer.Add(cookies)
EndIf
hRqst.ContentType ="application/x-www-form-urlencoded"
hRqst.Headers.Add("Accept-Language", "zh-cn")
Dim streamData As Stream
Dim bt() AsByteIf (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()
EndIfDim hRsp As HttpWebResponse
hRsp = hRqst.GetResponse()
streamData = hRsp.GetResponseStream()
If (hasCookie =TrueAndAlso (Not hRsp.Cookies IsNothing)) Then
cookies.Add(hRsp.Cookies)
EndIfIf (encType ="") Then
encType ="GB2312"EndIfDim readStream AsNew IO.StreamReader(streamData, System.Text.Encoding.GetEncoding(encType))
GetPage = readStream.ReadToEnd()
streamData.Close()
End FunctionPublicFunction GetPage()Function GetPage(ByVal url AsString, ByRef postPara As System.Collections.Hashtable, ByRef cookies As CookieCollection, ByVal hasCookie AsBoolean, OptionalByRef encType AsString="GB2312") AsStringReturn GetPage(url, ColToStr(postPara), cookies, True, encType)
End FunctionPublicFunction GetPage()Function GetPage(ByVal url AsString, ByRef cookies As CookieCollection, ByVal hasCookie AsBoolean, OptionalByRef encType AsString="GB2312") AsStringReturn GetPage(url, "", cookies, True, encType)
End Function'該函式用於轉換表單項集合為字串PublicSharedFunction ColToStr()Function ColToStr(ByRef ht As System.Collections.Hashtable, OptionalByRef encType AsString="GB2312") AsStringDimstrAsStringDim para As DictionaryEntry
ForEach 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&="&"Nextstr=str.Substring(0, str.Length -1)
ReturnstrEnd FunctionEnd Class
如果需要支援cookie,並支援refer,可以通過下面這個類來使用上面的httpdriver。
'該類用於訪問含有cookie的頁面Imports System.IO
PublicClass UserAgentClass UserAgent
Private m_cookies AsNew System.Net.CookieCollection
Private refer AsStringPrivate hd AsNew HttpDriver
PublicFunction GetPage()Function GetPage(ByVal url AsString, OptionalByRef postPara AsString="", OptionalByRef encType AsString="GB2312") AsString
GetPage = hd.GetPage(url, postPara, m_cookies, True, encType, refer)
refer = url
End FunctionPublicFunction GetPage()Function GetPage(ByVal url AsString, ByRef postPara As Hashtable, OptionalByRef encType AsString="GB2312") AsStringReturn GetPage(url, hd.ColToStr(postPara), encType)
End FunctionPublicFunction SetCookie()Function SetCookie(ByVal cookies As System.Net.CookieCollection)
m_cookies = cookies
End FunctionPublicFunction GetCookie()Function GetCookie() As System.Net.CookieCollection
Return m_cookies
End FunctionEnd Class
輕量的get網頁的函式
PublicFunction GetPage()Function GetPage(ByVal url AsString) AsStringDim 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 AsNew IO.StreamReader(streamData, System.Text.Encoding.GetEncoding("GB2312"))
GetPage = readStream.ReadToEnd()
streamData.Close()
End Function
Get方法:
Dim ua asNew UserAgent
Dim content asStringDim url asString
url ="www.sina.com.cn"
content = ua.GetPage(url)
Post方法:
Dim ua asNew UserAgent
Dim content asStringDim url asStringDim ht asNew HashTable
url ="mail.sina.com.cn"
ht.Add("username", "使用者名稱")
ht.Add("password", "密碼")
content = ua.GetPage(url, ht)
Imports System.IO
PublicClass HttpDriverClass HttpDriver
PublicFunction GetPage()Function GetPage(ByVal url AsString, OptionalByRef postPara AsString="", OptionalByRef encType AsString="GB2312") AsStringReturn GetPage(url, postPara, Nothing, False, encType)
End FunctionPublicFunction
End FunctionPublicFunction GetPage()Function GetPage(ByVal url AsString, ByRef postPara AsString, ByRef cookies
url ="http://"& url
EndIfDim hRqst As HttpWebRequest = HttpWebRequest.Create(url)
If (hasCookie =TrueAndAlso
hRqst.CookieContainer =New CookieContainer
hRqst.CookieContainer.Add(cookies)
EndIf
hRqst.ContentType ="application/x-www-form-urlencoded"
hRqst.Headers.Add("Accept-Language", "zh-cn")
Dim streamData As Stream
Dim bt() AsByteIf (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()
EndIfDim hRsp As HttpWebResponse
hRsp = hRqst.GetResponse()
streamData = hRsp.GetResponseStream()
If (hasCookie =TrueAndAlso (Not hRsp.Cookies IsNothing)) Then
cookies.Add(hRsp.Cookies)
EndIfIf (encType ="") Then
encType ="GB2312"EndIfDim readStream AsNew IO.StreamReader(streamData, System.Text.Encoding.GetEncoding(encType))
GetPage = readStream.ReadToEnd()
streamData.Close()
End FunctionPublicFunction GetPage()Function GetPage(ByVal url AsString, ByRef postPara As System.Collections.Hashtable, ByRef cookies As CookieCollection, ByVal hasCookie AsBoolean, OptionalByRef encType AsString="GB2312") AsStringReturn GetPage(url, ColToStr(postPara), cookies, True, encType)
End FunctionPublicFunction GetPage()Function GetPage(ByVal url AsString, ByRef cookies As CookieCollection, ByVal hasCookie AsBoolean, OptionalByRef encType AsString="GB2312") AsStringReturn GetPage(url, "", cookies, True, encType)
End Function'該函式用於轉換表單項集合為字串PublicSharedFunction ColToStr()Function ColToStr(ByRef ht As System.Collections.Hashtable, OptionalByRef encType AsString="GB2312") AsStringDimstrAsStringDim para As DictionaryEntry
ForEach 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&="&"Nextstr=str.Substring(0, str.Length -1)
ReturnstrEnd FunctionEnd Class
如果需要支援cookie,並支援refer,可以通過下面這個類來使用上面的httpdriver。
'該類用於訪問含有cookie的頁面Imports System.IO
PublicClass UserAgentClass UserAgent
Private m_cookies AsNew System.Net.CookieCollection
Private refer AsStringPrivate hd AsNew HttpDriver
PublicFunction GetPage()Function GetPage(ByVal url AsString, OptionalByRef postPara AsString="", OptionalByRef encType AsString="GB2312") AsString
GetPage = hd.GetPage(url, postPara, m_cookies, True, encType, refer)
refer = url
End FunctionPublicFunction GetPage()Function GetPage(ByVal url AsString, ByRef postPara As Hashtable, OptionalByRef encType AsString="GB2312") AsStringReturn GetPage(url, hd.ColToStr(postPara), encType)
End FunctionPublicFunction SetCookie()Function SetCookie(ByVal cookies As System.Net.CookieCollection)
m_cookies = cookies
End FunctionPublicFunction GetCookie()Function GetCookie() As System.Net.CookieCollection
Return m_cookies
End FunctionEnd Class
輕量的get網頁的函式
PublicFunction GetPage()Function GetPage(ByVal url AsString) AsStringDim 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 AsNew IO.StreamReader(streamData, System.Text.Encoding.GetEncoding("GB2312"))
GetPage = readStream.ReadToEnd()
streamData.Close()
End Function
Get方法:
Dim ua asNew UserAgent
Dim content asStringDim url asString
url ="www.sina.com.cn"
content = ua.GetPage(url)
Post方法:
Dim ua asNew UserAgent
Dim content asStringDim url asStringDim ht asNew HashTable
url ="mail.sina.com.cn"
ht.Add("username", "使用者名稱")
ht.Add("password", "密碼")
content = ua.GetPage(url, ht)