实现原理
--------------------------------
http://leidiqiu.iteye.com/blog/865145
一.模拟浏览器,发送 POST 请求,点击“登录”按钮的时候,浏览器完成以上动作,这里由代码自动完成
1.获取登录的sid和formhash
获得请求页面:http://192.168.72.130/bbs/logging.php?action=login
服务器端返回sid和formash
sid是会话的id,在返回的cookie中,读取头部的set-cookie部分即可
formhash在返回的文本文件中,http://192.168.72.130/bbs/logging.php?action=login可以看到登录界面,查看源文件找到登录的那个表单form,其中的input就有formhash,type=hidden
form为8位字母数字搭配,服务器返回给客户端的身份标识,具体算法好像是根据时间戳等信息,求MD5后,取其中8位
2.登录获取权限auth值
auth值通过服务器返回的cookie中获得
登录的过程:发送post请求,首先要将sid写入到cookie中,POST 的内容可以通过在 URL 中传输,或是在 POST 的文本内容中,传递的参数有 action, loginfield, questionid, answer, loginsubmit, formhash, username, password 等,相互之间用 & 连接。
此时,服务器就认可了用户已经登录,将会保存用户已登录的信息,服务端回应的 Cookie 中,包含了 auth 值,需要从 Cookie 中提取并保存,作为用户的身份标识,用作后续的发帖身份认证。
二.模拟浏览器发帖
3.获取发帖的formhash
录到服务器后,要进行发帖操作,需要得到一个 formhash 标识,通过 GET 请求页面 http://192.168.72.130/bbs/post.php?action=newthread&fid=10 ,其中包含了 fromhash 值,这个请求要包含 sid 和 auth 的 Cookie,其中 fid=10 意为版块的 ID ,可以通过登录具体的版块发帖,鼠标放在“发帖”按钮上,浏览器状态栏会显示 ID 号,或者在发帖页面,查看源文件,找到发帖的表单 form ,查看这个 ID 号。 服务器返回的内容中,会有个表单,包含 formhash 值,至此,就可以进行发帖操作了。
发帖操作和登录操作类似,通过 POST 请求页面 http://192.168.72.130/bbs/post.php,同样需要把 Cookies 的内容加上;具体 POST 的内容,可以参考发帖操作页面的源代码,其中 form 的内容就是要 POST 的内容。可以参考如下: "action=newthread&topicsubmit=yes&subject=" + subject + "&formhash=" + post_formhash + "&fid=" + new_fid + "&message=" + msg
三.总结
至此,自动发帖的原理就介绍完了。 基本的过程就是,GET 请求登录页面,Cookie 中获得 sid ,返回页面中获得 formhash,用 sid formhash username ... 等信息,发送 POST 请求登录,登录后 Cookie 中会有 auth 等信息,取得 auth 后,GET 请求发帖页面,在页面中获得 formhash ,再以 sid auth ...等发送 POST 请求,完成发帖操作。 如果有笔误或讲得不对的地方,请网友指教。 附上 wireshark 网络抓包的部分内容,供参考。
Expert Info (Chat/Sequence): GET /bbs/logging.php?action=login HTTP/1.1\r\n HTTP/1.1 200 OK\r\n CsN_sid=RkWOSy; expires=Thu, 13-Jan-2011 09:26:26 GMT; path=/; httponly <input type="hidden" name="formhash" value="dc5dc9b8" />\r\n Expert Info (Chat/Sequence): POST /bbs/logging.php HTTP/1.1\r\n CsN_sid=RkWOSy action=login&formhash=dc5dc9b8&loginfield=username&questionid=0&answer=&loginmode=normal&loginsubmit=true&username=test&password=test HTTP/1.1 200 OK\r\n CsN_auth=0d5eTSofQlbIT7ENDULLpdt%2FCi3ysya4vt3LHVIVY%2BRnUAsxR5Gx8R4TOmcS7yXSVjsJ1BUG%2BVb0THYVj5YagA; expires=Sun, 03-Jan-2021 09:26:37 GMT; path=/; httponly <a href="logging.php?action=logout&formhash=dc5dc9b8">\315\313\263\366</a>\r\n GET /bbs/post.php?action=newthread&fid=10&extra=page%3D1 HTTP/1.1\r\n CsN_sid=RkWOSy; CsN_auth=0d5eTSofQlbIT7ENDULLpdt%2FCi3ysya4vt3LHVIVY%2BRnUAsxR5Gx8R4TOmcS7yXSVjsJ1BUG%2BVb0THYVj5YagA HTTP/1.1 200 OK\r\n <input type="hidden" name="formhash" id="formhash" value="570a67c4" />\r\n <input type="hidden" name="posttime" id="posttime" value="1294306004" />\r\n <input type="hidden" name="wysiwyg" id="e_mode" value="0" />\r\n <input type="hidden" name="iconid" id="iconid" value="" />\r\n POST /bbs/post.php HTTP/1.1\r\n CsN_sid=RkWOSy; CsN_auth=0d5eTSofQlbIT7ENDULLpdt%2FCi3ysya4vt3LHVIVY%2BRnUAsxR5Gx8R4TOmcS7yXSVjsJ1BUG%2BVb0THYVj5YagA action=reply&replysubmit=yes&replysubmit=true&formhash=570a67c4&fid=10&tid=15&subject=&message=6666666666666666666666666 HTTP/1.1 200 OK\r\n CsN_oldtopics=D15D; expires=Thu, 06-Jan-2011 10:27:06 GMT; path=/ CsN_fid10=1294306025; expires=Thu, 06-Jan-2011 10:27:06 GMT; path=/ CsN_visitedfid=10; expires=Sat, 05-Feb-2011 09:27:06 GMT; path=
C#实现
----------------------------------
【用HttpWebRequest中发送GET/HTTP/HTTPS请求】----GetRequst和GetRespose的类封装
http://zhoufoxcn.blog.51cto.com/792419/561934
CookieCollection cc =new CookieCollection();
string s = "SID=ARRGy4M1QVBtTU-ymi8bL6X8mVkctYbSbyDgdH8inu48rh_7FFxHE6MKYwqBFAJqlplUxq7hnBK5eqoh3E54jqk=;Domain=.google.com;Path=/,LSID=AaMBTixN1MqutGovVSOejyb8mVkctYbSbyDgdH8inu48rh_7FFxHE6MKYwqBFAJqlhCe_QqxLg00W5OZejb_UeQ=;Domain=www.google.com;Path=/accounts";
Regex re = new Regex("([^;,]+)=([^;,]+);Domain=([^;,]+);Path=([^;,]+)", RegexOptions.IgnoreCase);
foreach (Match m in re.Matches(s))
{
//name, value, path, domain
Cookie c = new Cookie(m.Groups[1].Value, m.Groups[2].Value,m.Groups[3].Value,m.Groups[3].Value);
cc.Add(c);
}
【C#三种模拟自动登录和提交POST信息的实现方法】http://www.cnblogs.com/xiangboren/archive/2009/03/06/1404473.html---读取html到txt文件中
【JAVA 编写代码自动在 discuz 7.2 的论坛上发帖子】http://leidiqiu.iteye.com/blog/861103
--------------------------------------------
模拟登录原创阅读网
private string accept = "*/*"; private string contentType = "application/x-www-form-urlencoded"; private string refer = "http://space.yuanchuang.com/account/login?ReturnUrl=http%3A//bbs.yuanchuang.com/forum.php"; private string userAgent = "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1171.0 Safari/537.1"; private CookieContainer cookies = new CookieContainer(); private void button1_Click(object sender, EventArgs e) { string forWard = "http://bbs.yuanchuang.com/forum.php"; string userName = "123456"; //帐号 string passWord = "123456; //密码 string url = "http://space.yuanchuang.com/account/login"; string postData = string.Format("forward={0}&username={1}&password={2}", forWard, userName, passWord); textBox1.Text = GetPost(url, "post", postData);//获取源代码 } #region 获取数据方法 public string GetPost(string url, string method, string data) { Uri uri = new Uri(url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.UserAgent = this.userAgent; request.Accept = this.accept; request.ContentType = this.contentType; request.Method = method; request.Referer = this.refer; request.CookieContainer = this.cookies; if (method.Equals("post")) { byte[] byteRequest = Encoding.Default.GetBytes(data); Stream rs = request.GetRequestStream(); rs.Write(byteRequest, 0, byteRequest.Length); rs.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); cookies.Add(response.Cookies); Stream resultStream = response.GetResponseStream(); StreamReader sr = new StreamReader(resultStream, Encoding.UTF8); string html = sr.ReadToEnd(); sr.Close(); resultStream.Close(); request.Abort(); response.Close(); return html; } #endregion
---------------------------------------------------------------------------
百度知道
http://zhidao.baidu.com/link?url=0Fw0XU-QEXPZWJrOf7hgiAJhwplXZ_ZCggJTZnEmht_4RuIc_6Q7iOFKqu6RR1pswB4_CZhk4DBwYlJKRqLPxq
----------------------------------------------------------------------------
winform进行post数据httpwebrequest
http://www.cnblogs.com/lyl6796910/p/3722280.html
http://bbs.csdn.net/topics/390593264
-----------------------------------------------------------------------------
string defaultUserAgent =
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
Uri uri = new Uri("http://www.fjptma.org/bbs/member.php?mod=logging&action=login&loginsubmit=yes");
//获取refer页的sid和formhash值
HttpWebRequest requestGet = WebRequest.Create("http://www.fjptma.org/bbs/forum.php") as HttpWebRequest;
requestGet.Method = "GET";
requestGet.UserAgent = defaultUserAgent;
HttpWebResponse responseGet = requestGet.GetResponse() as HttpWebResponse;
string temp = responseGet.Headers["Set-Cookie"];//---此处没找到包含sid cookie的键IqJj_2132_sid。。。。。。。。。。。。
int pos1 = temp.IndexOf("IqJj_2132_sid=");
string cookie_sid = temp.Substring(pos1+14, 6);
Cookie cookieSid=new Cookie("IqJj_2132_sid",cookie_sid);
Stream stream = responseGet.GetResponseStream();
StreamReader streamReader=new StreamReader(stream,Encoding.Default);
string htmlRefer = streamReader.ReadToEnd();
int pos = htmlRefer.IndexOf("name=\"formhash\" value=");
string login_formhash = htmlRefer.Substring(pos + 23, 8);
//构建请求正文
string postdata = "mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&username=test1&password=123";
//构建请求头
HttpWebRequest request= (HttpWebRequest) WebRequest.Create("http://www.fjptma.org/bbs/member.php");
request.Accept = "text/plain, */*; q=0.01";
request.Referer = "http://www.fjptma.org/bbs/forum.php";
request.UserAgent = defaultUserAgent;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.Headers.Add("Accept-Encoding", "gzip, deflate");
byte[] postdatabytes = Encoding.Default.GetBytes(postdata);
request.ContentLength = postdatabytes.Length;
CookieContainer container=new CookieContainer();
container.Add(uri, new Cookie("IqJj_2132_sid", cookie_sid));
request.CookieContainer = container;
//将请求正文写入到请求流当中
Stream postStream= request.GetRequestStream();
//byte[] postData = Encoding.Default.GetBytes("action=login&loginfield=username&questionid=0&answer=&loginsubmit=yes&formhash="
// + login_formhash
// + "&username="
// + name
// + "&password=" + pwd);
postStream.Write(postdatabytes,0,postdatabytes.Length);
postStream.Close();
//获取响应对象,判断是否登录成功
string result = "";
HttpWebResponse response=(HttpWebResponse)request.GetResponse();
#region
//判断响应的信息是否为压缩信息 若为压缩信息解压后返回
if (response.ContentEncoding == "gzip")
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
byte[] buffer = new byte[1024];
int l = zip.Read(buffer, 0, buffer.Length);
while (l > 0)
{
ms.Write(buffer, 0, l);
l = zip.Read(buffer, 0, buffer.Length);
}
ms.Dispose();
zip.Dispose();
result = Encoding.Default.GetString(ms.ToArray());
}
#endregion
Stream stream2 = response.GetResponseStream();
StreamReader sr=new StreamReader(stream2,Encoding.Default);
string html = sr.ReadToEnd();
if (html.Contains("新听众"))
{
MessageBox.Show("登录成功");
}
else
{
MessageBox.Show("登录失败");
}
=======================================
string postdata = "mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&username=test1&password=123";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.fjptma.org/bbs/member.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = "http://www.fjptma.org/bbs/forum.php";
byte[] postdatabytes = Encoding.Default.GetBytes(postdata);
request.ContentLength = postdatabytes.Length;
request.AllowAutoRedirect = false;
CookieContainer cc = new CookieContainer();
request.CookieContainer = cc;
request.KeepAlive = true;
//提交请求
Stream stream = request.GetRequestStream();
stream.Write(postdatabytes, 0, postdatabytes.Length);
stream.Close();
//接收响应
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//保存返回cookie
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies;
string Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
string content = sr.ReadToEnd();
response.Close();
==============================
发帖
http://q.cnblogs.com/q/39832/
模拟网站签到问题
http://www.sufeinet.com/forum.php?mod=viewthread&tid=7547&page=1
=========================================
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Text.RegularExpressions;
namespace PTBBSAutoSend { public partial class Form1 : Form { private string Username { get; set; } private string Password { get; set; } public Form1() { InitializeComponent(); Username = txtUserName.Text.Trim(); Password = txtPassword.Text.Trim(); }
private void btnPTDSXH_Click(object sender, EventArgs e) { //Login string postdata = string.Format( "mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&username={0}&password={1}", Username, Password); HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://www.fjptma.org/bbs/member.php"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Referer = "http://www.fjptma.org/bbs/forum.php"; request.AllowAutoRedirect = false; byte[] postdatabytes = Encoding.Default.GetBytes(postdata); request.ContentLength = postdatabytes.Length; CookieContainer cc = new CookieContainer(); request.CookieContainer = cc; request.KeepAlive = true;
Stream stream = request.GetRequestStream(); stream.Write(postdatabytes, 0, postdatabytes.Length); stream.Close();
HttpWebResponse response = (HttpWebResponse) request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); string htmlLoginSuccess = sr.ReadToEnd();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri); CookieCollection cook = response.Cookies; string cookieStr = request.CookieContainer.GetCookieHeader(request.RequestUri);
string referhtml = GetHtml(cook, "http://www.fjptma.org/bbs/forum.php?mod=post&action=newthread&fid=37"); Encoding encoding = Encoding.GetEncoding("gb2312"); int referFormhashPos = referhtml.IndexOf("name=\"formhash\" value="); string referFormhash = referhtml.Substring(referFormhashPos + 23, 8); int referPosttimePos=referhtml.IndexOf("name=\"posttime\" value="); string referPosttime = referhtml.Substring(referPosttimePos + 23, 10); string subject = "廊坊干果销售"; string message = "有需要的联系csharphuang,小米、爆米花、干杏仁等等,仅限廊坊附近,量大从优"; //send IDictionary<string,string> parameters=new Dictionary<string, string>(); parameters.Add("mod","post"); parameters.Add("action","newthread"); parameters.Add("fid","37"); parameters.Add("topicsubmit","yes"); parameters.Add("formhash",referFormhash); parameters.Add("posttime",referPosttime); parameters.Add("wysiwyg","1"); parameters.Add("subject",subject); parameters.Add("message",message);
HttpWebResponse responseSend= HttpWebResponseUtility.CreatePostHttpResponse("http://www.fjptma.org/bbs/forum.php", parameters, null, null, encoding, cook); string htmlSendSuccess = GetHtml(responseSend); if (htmlSendSuccess.Contains(subject)) { MessageBox.Show("发布成功!地址为:"+responseSend.ResponseUri); }
var _t = 0; }
private string GetHtml(CookieCollection cookies,string urlhandler) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlhandler); request.Method = "GET"; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.Default); return reader.ReadToEnd(); }
private string GetHtml(HttpWebResponse response) { StreamReader sr=new StreamReader(response.GetResponseStream(),Encoding.Default); return sr.ReadToEnd(); } } }