用来获取网页的类
来源:互联网 发布:杜兰特每个赛季的数据 编辑:程序博客网 时间:2024/05/21 00:18
这个经过测试,使用上比较稳定,因为考虑到统一的错误处理,类里面没有catch任何错误,所有网络错误都在使用的时候捕获,以便决定重试或终止。支持get和post,支持自定义编码,支持cookie,但不支持上传文件。
如果需要支持cookie,并支持refer,可以通过下面这个类来使用上面的httpdriver。
轻量的get网页的函数
轻量的get网页的函数
Get方法:
Post方法(页面和参数名字都是示例)。UserAgent的好处是可以透明的保存cookie,用以下的方法就可以实现登录后保存登录信息了。
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
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
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
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网页的函数
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
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)
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)
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)
- 用来获取网页的类
- 用来获取网页的类
- vb.net用来获取网页的类
- 用来获取网页的类(转!vb.net参考)
- 用来获取网页的类(转!vb.net参考)
- 用来获取网页的类(转!vb.net参考)
- DAO(DataBaseConnection)(用来获取数据库连接的类)
- Linux下用来获取各种系统信息的C++类
- Linux下用来获取各种系统信息的C++类
- 可以用来做ppt的网页效果
- DisplayMetrics用来获取屏幕的宽高
- 用来获取指定类的所有祖先类的名称及其所在的单元名称。
- lazyload-min.js 用来加速网页的一个小运用
- 网页第一行的doctype是用来做什么的
- document.domain 用来得到当前网页的域名。
- 获取网页某一类的链接
- 一个用来获取硬盘信息工具的网站
- 获取网页的标题
- JAVA大学实用教程实验指导5.2.1答案
- 用PerfHUD来调试商业游戏
- 在窗体上移动控件
- dfdf
- 利用正则表达式去掉html代码
- 用来获取网页的类
- 如何将自己的程序设置为默认浏览器
- 如何利用socket进行HTTP访问
- 常用数值计算库
- 多表联接更新数据
- 一个Socket网络编程的简单例子
- 23之后的男人该看看
- 学而不思则罔,思而不学则殆
- 安装ORACAL时的错误提示:加载数据库时出错:areasQueries