Public Function getDataAsHtml(ByVal url As String, ByVal postData As String, ByVal modifyCook As Boolean)
'构造请求头
Try
Dim httpUrl As New System.Uri(url) '生成URL类
Dim req As HttpWebRequest '创建HTTP请求实例
req = CType(WebRequest.Create(httpUrl), HttpWebRequest)
req.CookieContainer = currentCookies
req.Timeout = 600000
'构造POST数据
req.Method = "POST" '设置请求方式
req.ContentType = "application/x-www-form-urlencoded"
Dim ParameterEncoding As Encoding = System.Text.Encoding.GetEncoding("GB2312") '设置编码方式
Dim bytesData As Byte() = ParameterEncoding.GetBytes(postData) '获取POST数据
req.ContentLength = bytesData.Length '获取数据长度
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Maxthon; SV1; .NET CLR 1.1.4322)"
req.Accept = "*.*"
req.ProtocolVersion = HttpVersion.Version11
'发送HTTP请求
Dim postStream As Stream = req.GetRequestStream()
postStream.Write(bytesData, 0, bytesData.Length) '以上向服务器post信息。
postStream.Close()
'读取返回信息
Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse) '以下获取服务器返回信息
Dim reader As StreamReader = _
New StreamReader(res.GetResponseStream, System.Text.Encoding.GetEncoding("GB2312"))
currentHTML = reader.ReadToEnd()
'测试返回信息
'MsgBox(currentHTML)
If modifyCook = True Then
Dim cook As Cookie
For Each cook In res.Cookies
currentCookies.Add(cook)
If cook.Name = "JSESSIONID" Then
sessionID = cook.Value.Substring(4, 23)
End If
If cook.Name = "EBankNetBank2001" Then
usera = cook.Value
End If
Next cook
End If
'关闭
res.Close()
Return currentHTML
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
参数说明,url---网址,postData---所要执行的动作及参数,modifyCook---是否改变本地的cookies。