1、登陆
登陆我们可以直接用WebRequest.Create(url)的方法,POST数据即可,这里需要注意的就是post的数据必须编码一下,文本是不能直接post的
2、取得cookie
如果你想把cookie显示出来,需要使用GetResponseHeader(”Set-Cookie”)将cookie以文本字段显示
如果你要发布文章时也提交cookie,直接提交GetResponseHeader(”Set-Cookie”)是不行的,因为他是string的文本字段,而HttpWebResponse的CookieContainer确实cookie数组
这时你需要首先定义一个空白的CookieContainer,登陆时就会得到cookie,提交文章直接使用这个值即可
源码部分如下:
以下为网上找到的测试源码,不是vb.net的,不过说明了原理
CookieContainer cc = new CookieContainer();
for(int i=0;i<100;i++)
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(”http://localhost/AspxApp/MainForm.aspx”);
myReq.CookieContainer = cc;
HttpWebResponse resp = myReq.GetResponse() as HttpWebResponse;
Stream s = resp.GetResponseStream();
StreamReader sr = new StreamReader(s); String text = sr.ReadToEnd();
sr.Close();
s.Close();
}
我写的代码很乱,而且只是为了测试,所以看起来很繁琐,效率很差,大家将就看一下吧
Imports System.net
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Text
Public Class Form1
Private requestScrape As HttpWebRequest
Private responseScrape As HttpWebResponse
Private cc As New CookieContainer
Function GetHttpStream(ByVal url As String, ByVal uname As String, ByVal upass As String) As Stream
‘ 使用 WebRequestFactory 创建请求。
Dim encoding = New ASCIIEncoding
Dim aa As String, ab As Byte()
aa = “username=” & uname & “&password=” & upass & “&loginsubmit=true”
ab = encoding.GetBytes(aa)
‘TextBox3.Text = aa
requestScrape = CType(WebRequest.Create(url), HttpWebRequest)
With requestScrape
.UserAgent = “Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)”
.Method = “POST”
.ContentType = “application/x-www-form-urlencoded”
.ContentLength = ab.Length
.Timeout = 10000
.CookieContainer = cc
End With
Dim newStream As Stream = requestScrape.GetRequestStream()
newStream.Write(ab, 0, ab.Length)
newStream.Close()
Try
‘ 返回响应流。
responseScrape = CType(requestScrape.GetResponse(), HttpWebResponse)
Return responseScrape.GetResponseStream()
Catch exp As Exception
‘ 因为错误最有可能是由键入错误的 Url 或者
‘ 没有 Internet 连接造成的,所以创建一条转发回调用函数的
‘ 自定义错误信息。
Throw New Exception(exp.Message.ToString)
End Try
End Function
‘ 此函数读取由 httpWebResponse 对象返回的流,并
‘ 将其转换为字符串供 RegEx 处理。
Function ConvertStreamToString(ByVal stmSource As Stream, ByVal codeb As Integer) As String
Dim sr As StreamReader = Nothing
If Not IsNothing(stmSource) Then
Try
sr = New StreamReader(stmSource, System.Text.Encoding.GetEncoding(codeb))
‘ 使用设定的编码格式读取并返回流的全部内容。
Return sr.ReadToEnd
Catch exp As Exception
‘ 不显示 MsgBox。只是转发
‘来自 GetHttpStream() 的错误信息。
Throw New Exception()
Finally
‘ 清理 Stream 和 StreamReader。
responseScrape.Close()
sr.Close()
End Try
End If
Return Nothing
End Function
Function GetHttpStream2(ByVal url As String) As Stream
‘ 使用 WebRequestFactory 创建请求。
requestScrape = CType(WebRequest.Create(url), HttpWebRequest)
With requestScrape
.UserAgent = “Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)”
.Method = “GET”
.CookieContainer = cc
.Timeout = 10000
End With
Try
‘ 返回响应流。
responseScrape = CType(requestScrape.GetResponse(), HttpWebResponse)
Return responseScrape.GetResponseStream()
Catch exp As Exception
‘ 因为错误最有可能是由键入错误的 Url 或者
‘ 没有 Internet 连接造成的,所以创建一条转发回调用函数的
‘ 自定义错误信息。
Throw New Exception(exp.Message.ToString)
End Try
End Function
‘ 此函数读取由 httpWebResponse 对象返回的流,并
‘ 将其转换为字符串供 RegEx 处理。
Function ConvertStreamToString2(ByVal stmSource As Stream, ByVal codeb As Integer) As String
Dim sr As StreamReader = Nothing
If Not IsNothing(stmSource) Then
Try
sr = New StreamReader(stmSource, System.Text.Encoding.GetEncoding(codeb))
‘ 使用设定的编码格式读取并返回流的全部内容。
Return sr.ReadToEnd
Catch exp As Exception
‘ 不显示 MsgBox。只是转发
‘来自 GetHttpStream() 的错误信息。
Throw New Exception()
Finally
‘ 清理 Stream 和 StreamReader。
responseScrape.Close()
sr.Close()
End Try
End If
Return Nothing
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ystrm As Stream = GetHttpStream(”http://127.0.0.1/login.asp”, TextBox1.Text, TextBox2.Text) ‘获取网址
Dim respHTML As String = ConvertStreamToString(ystrm, 65001)
RichTextBox1.Text = respHTML.ToString
TextBox3.Text = responseScrape.GetResponseHeader(”Set-Cookie”).ToString
Dim ystrm2 As Stream = GetHttpStream2(”http://127.0.0.1/post.asp”) ‘获取网址
Dim respHTML2 As String = ConvertStreamToString2(ystrm2, 65001)
RichTextBox1.Text = respHTML2.ToString
End Sub
End Class
通过比较RichTextBox1.Text的内容判断是否正常发送cookie
65001是UTF8编码,一般用0即可
本文作者:佚名 来源:http://www.lob.cn/