博客大巴,自动登录,并发布信息开发小计。
工具准备:Fiddler
相关网页:
登录页面:http://passport.blogbus.com/login
信息发布信息:http://www.blogbus.com/user/?blogid=49xxx944&mm=Post&aa=SaveAdd&page=&outputmode=1
登录页面,比较简单,使用Post提交username,password即可!判断是否成功,采用判断CookieContainer的Count属性是否大于0
相关代码如下:
代码
Dim cc As New System.Net.CookieContainer
Dim postData As String = String.Format("username={0}&password={1}", "xxx", "xxxx")
Dim postByte() As Byte = System.Text.Encoding.UTF8.GetBytes(postData)
Dim wr As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("http://passport.blogbus.com/login"), Net.HttpWebRequest)
wr.CookieContainer = cc
wr.Method = "POST"
wr.ContentType = "application/x-www-form-urlencoded"
wr.ContentLength = postByte.Length
Dim sr = wr.GetRequestStream
sr.Write(postByte, 0, postByte.Length)
sr.Close()
Dim rs As System.Net.HttpWebResponse = CType(wr.GetResponse, Net.HttpWebResponse)
Dim sr2 As New System.IO.StreamReader(rs.GetResponseStream, System.Text.Encoding.UTF8)
Dim result As String = sr2.ReadLine
Console.WriteLine(result)
登录成功后,需要进行信息发布:
这里走了一点弯路,最开始一直没有设置WebRequest的Referer属性,一直不能提交成功。
相关代码如下:
代码
postData = "Title=ddd&PostTime=2009-12-25+17%3A45%3A14&SortID=0&tContent=%3Cp%3Edddd%3C%2Fp%3E&Tags=ddd&Excerpt=&Trackback=%E6%AF%8F%E8%A1%8C%E8%BE%93%E5%85%A5%E4%B8%80%E6%9D%A1%E5%BC%95%E7%94%A8%E5%9C%B0%E5%9D%80&ID=&DraftId=0&force=0"
postByte = System.Text.Encoding.UTF8.GetBytes(postData)
wr = CType(System.Net.WebRequest.Create( http://www.blogbus.com/user/?blogid=4xxx4&mm=Post&aa=SaveAdd&page=&outputmode=1 ), Net.HttpWebRequest)
wr.CookieContainer = cc
wr.Referer = "http://www.blogbus.com/user/?mm=Post&aa=Add"
wr.ContentType = "application/x-www-form-urlencoded"
wr.ContentLength = postByte.Length
wr.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; GTB6.3; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
wr.KeepAlive = True
wr.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*"
wr.Method = "POST"
sr = wr.GetRequestStream
sr.Write(postByte, 0, postByte.Length)
sr.Close()
rs = CType(wr.GetResponse, Net.HttpWebResponse)
sr2 = New System.IO.StreamReader(rs.GetResponseStream, System.Text.Encoding.UTF8)
result = sr2.ReadToEnd
If result.IndexOf("提交成功") <> -1 Then
Console.WriteLine("提交成功")
Else
Console
之前没有写过,类于这样的代码。写了之后发现,验证码,才是最难搞的!