关于POST的问题:您的请求来路不正确,无法提交。 本帖最后由 ioirliao16 于 2011-06-30 23:40:40 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好好比对下抓包的结果吧,Headers里看看少什么了。 这段是我程序运行时候,Wireshark抓的包POST //post.php?action=reply&fid=19&tid=1222671&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1 HTTP/1.1Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*Referer: http://bbs.egou.com/viewthread.php?tid=1222671&extra=page%3D1User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS86335; .NET4.0C; TheWorld)Content-Type:application/x-www-form-urlencodedContent-Type: application/x-www-form-urlencodedHost: bbs.egou.comCookie: cfT_sid=d3tEE3; cfT_auth=3e16Nlcetojn1rORkfdf2fmAYKaYum%2FncGsg15xl%2BxdK6R3qNp3%2FGvAlTryIOkitXa%2F5IyffUlfGaMnk8D%2FBgY%2FBL9GhYw; egouUser=ZWdvb2dsZS8vZmEzNjFiOGZlNjVhMDM4NmY5NDBjYTE4Yjk2MGNhODMvLzEwNDQyMTAvL0AvLzhkMg%3D%3D; cfT_oldtopics=D1222290DContent-Length: 99Expect: 100-continue这是我用网站回复帖子时用WrieShark抓的包POST /post.php?action=reply&fid=19&tid=1222671&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1 HTTP/1.1Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*Referer: http://bbs.egou.com/viewthread.php?tid=1222671&extra=page%3D1Accept-Language: zh-cnUser-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS86335; .NET4.0C; TheWorld)Content-Type: application/x-www-form-urlencodedAccept-Encoding: gzip, deflateHost: bbs.egou.comContent-Length: 68Connection: Keep-AliveCache-Control: no-cacheCookie: __utma=18130278.2129965114.1309415671.1309479323.1309483287.9; __utmz=18130278.1309483287.9.9.utmcsr=bbs.egou.com|utmccn=(referral)|utmcmd=referral|utmcct=/forumdisplay.php; _adwp=131476444.3955851719.1309415675.1309479324.1309483289.9; Hm_lvt_977ed62600436aca38f88304752ba8c4=1309483289342; _adwt=1309483289; smile=5D1; cfT_sid=0t5K0k; __utmb=18130278.15.10.1309483287; _adwb=131476444; cfT_oldtopics=D1222671D; cfT_fid19=1309482480; __utmc=18130278; t=undefined; Hm_lpvt_977ed62600436aca38f88304752ba8c4=1309483410498; _adwc=131476444%23http%253A%252F%252Fbbs.egou.com%252Fforumdisplay.php%253Ffid%253D19%2526page%253D1; cfT_auth=ba82oK4WDznENUDW9CTAMVV9%2Bwq%2FPKKtXLPZXwezTq2pdiotiONvChbVco1c6CpkMpXapH%2BhyGuiwWqNIiXBj0plRSiJMQ; egouUser=ZWdvb2dsZS8vZmEzNjFiOGZlNjVhMDM4NmY5NDBjYTE4Yjk2MGNhODMvLzEwNDQyMTAvL0AvLzhkMg%3D%3D 1.你的Content-Length怎么多那么多?2.Cookie还少 Content-Length的问题我发现了,是程序发送的字节和我在论坛上发送的字节数目不同的,忘记修改为相同的。可是cookies就不明白为什么这样了。请指教。 这是全部代码struct WebInfo{ public string formhash; public CookieContainer cookieContainer; } WebInfo GetWebInfo(string url){ WebInfo webInfo=new MainForm.WebInfo(); HttpWebRequest webRequest=(HttpWebRequest)WebRequest.Create(url); webRequest.Method="GET"; CookieContainer cookieC=new CookieContainer(); cookieC.GetCookies(webRequest.RequestUri); webInfo.cookieContainer=cookieC; HttpWebResponse webResponse=(HttpWebResponse)webRequest.GetResponse(); Stream responseStream=webResponse.GetResponseStream(); StreamReader responseReader=new StreamReader(responseStream,Encoding.GetEncoding("gb2312")); string str=responseReader.ReadToEnd(); string formhash=str.Substring(str.IndexOf("formhash=")+9,8); webInfo.formhash=formhash; return webInfo; } void PostMessage(string text){ HttpWebRequest webRequest=WebRequest.Create("http://bbs.egou.com/viewthread.php?tid=1222290&extra=page%3D1") as HttpWebRequest; webRequest.Method="GET"; webRequest.CookieContainer=webInof.cookieContainer; HttpWebResponse tmpResponse=webRequest.GetResponse() as HttpWebResponse; Stream tmpStream=tmpResponse.GetResponseStream(); StreamReader tmpReader=new StreamReader(tmpStream,Encoding.GetEncoding("gb2312")); string referer=webRequest.GetResponse().ResponseUri.AbsoluteUri; webRequest=WebRequest.Create("http://bbs.egou.com//post.php?action=reply&fid=19&tid=1222671&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1") as HttpWebRequest; webRequest.Method="POST"; webRequest.Accept="image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*"; webRequest.Referer="http://bbs.egou.com/viewthread.php?tid=1222671&extra=page%3D1"; webRequest.UserAgent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS86335; .NET4.0C; TheWorld)Content-Type:application/x-www-form-urlencoded"; webRequest.ContentType="application/x-www-form-urlencoded"; webRequest.CookieContainer=webInof.cookieContainer; string pst="formhash="+webInof.formhash+"&subject=&usesig=1&message=%21%21%21%21%21%21%21%21%21%21%21%21%21%21%21%21%21"; byte[] b=Encoding.ASCII.GetBytes(pst); Stream requestStrem=webRequest.GetRequestStream(); requestStrem.Write(b,0,b.Length); requestStrem.Close(); HttpWebResponse webResponse=webRequest.GetResponse() as HttpWebResponse; Stream responseStream=webResponse.GetResponseStream(); StreamReader responseReader=new StreamReader(responseStream,Encoding.GetEncoding("gb2312")); richTextBox1.Clear(); richTextBox1.AppendText(responseReader.ReadToEnd()); } WebInfo webInof; void LoginBtnClick(object sender, EventArgs e) { webInof=GetWebInfo("http://bbs.egou.com/logging.php?action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=floatwin_login"); Encoding coder=Encoding.GetEncoding("utf-8"); string str="formhash="+webInof.formhash+"&loginfield=username&username="+ userText.Text+"&password="+passText.Text+ "&questionid=0&answer=&loginsubmit=true"; byte[] postByte=Encoding.ASCII.GetBytes(str); HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com/logging.php?action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=floatwin_login"); hwq.CookieContainer=webInof.cookieContainer; hwq.Method="POST"; hwq.ContentType="application/x-www-form-urlencoded"; hwq.Accept="application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; hwq.UserAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10"; hwq.Referer="http://bbs.egou.com/logging.php?action=login"; hwq.KeepAlive=true; hwq.ContentLength=postByte.Length; Stream postStream=hwq.GetRequestStream(); postStream.Write(postByte,0,postByte.Length); postStream.Close(); HttpWebResponse wrp=(HttpWebResponse)hwq.GetResponse(); Stream receiveStream=wrp.GetResponseStream(); StreamReader reader=new StreamReader(receiveStream,Encoding.GetEncoding("gb2312")); string s=reader.ReadToEnd(); receiveStream.Close(); richTextBox1.Clear(); richTextBox1.AppendText(s); PostMessage("xx"); } <?xml version="1.0" encoding="gbk"?><root><![CDATA[对不起,本版块只有特定用户组可以回复。<script type="text/javascript" reload="1">if(typeof messagehandle_fastpost =='function') {messagehandle_fastpost('HALTED', '对不起,本版块只有特定用户组可以回复。');}</script>]]></root>我搞了下,提示这个,可能是没有该版块回复的权限.. 1:设置request一下重定向问题,试一试,那个属性忘了。2:其实可以通过TcpClient直接写入,你抓取到的http头文件。 因为您没用帐号,我用个测试帐号:tempUser 123456789 NetworkCredential^ myCredentials = gcnew NetworkCredential( "MSO","changeme" );request->Credentials = myCredentials;request->PreAuthenticate = true;//必須要1:确定一下有没有 账号密码 然后做如上设置2:实在不行 就有TcpClient 直接写入http头文件吧 人家只要校验一下url或者cookie中某个编号有没有过期就行了,胡乱给你个“你的来路不正”的假说明,也是可以的。 哪个版块可以回帖?你现在的tempUser好像权限特低? 我自己是用了一个比较高权限的帐号测试的,不会有tempUser的问题。我私信一个帐号与您好哦。 发帖的时候也要先 GET: viewthread 拿到 hash ,再 post。下面代码,是回复 http://bbs.egou.com/viewthread.php?tid=1202032&extra=&highlight=&page=71的。你拷贝到 Console 工程里就能运行。using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Web;using System.IO;using System.Text.RegularExpressions;namespace HttpWebRequest2{ class Program { static void Main(string[] args) { Login("饿狗传说", "19850420"); var reply = "thanks by JustACoder " + DateTime.Now.ToString("yyyyMMdd HHmmss"); PostReply("1202032", reply); Console.Read(); } private static CookieContainer _cookie = new CookieContainer(); static string GetFormHash(string content) { Regex regex = new Regex("name=\"formhash\" value=\"(.*?)\""); return regex.Match(content).Groups[1].Value; } static void Login(string username, string passwd) { var encoding = Encoding.GetEncoding("GBK"); var url = "http://bbs.egou.com/logging.php?action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=floatwin_login"; var req = WebRequest.Create(url) as HttpWebRequest; req.CookieContainer = _cookie; var resp = req.Get(); var result = resp.ReadAsString(encoding); var hash = GetFormHash(result); Console.WriteLine(hash); var body = new Dictionary<string, string>(); body.Add("loginfield", "username"); body.Add("username", username); body.Add("password", passwd); body.Add("formhash", hash); body.Add("questionid", "0"); body.Add("answer", ""); body.Add("loginsubmit", "true"); req = WebRequest.Create(url) as HttpWebRequest; req.CookieContainer = _cookie; req.Referer = "http://bbs.egou.com/logging.php?action=login"; resp = req.Post(body, encoding); result = resp.ReadAsString(encoding); Console.WriteLine(result); } static void PostReply(string tid, string content) { var encoding = Encoding.GetEncoding("GBK"); var url = "http://bbs.egou.com/viewthread.php?tid={0}&extra=&highlight=&page=1"; url = string.Format(url, tid); var req = WebRequest.Create(url) as HttpWebRequest; req.CookieContainer = _cookie; var resp = req.Get(); var result = resp.ReadAsString(encoding); var hash = GetFormHash(result); Console.WriteLine(hash); url = "http://bbs.egou.com/post.php?action=reply&fid=168&tid={0}&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1"; url = string.Format(url, tid); req = WebRequest.Create(url) as HttpWebRequest; req.CookieContainer = _cookie; var body = new Dictionary<string, string>(); body.Add("formhash", hash); body.Add("subject", ""); body.Add("usesig", "0"); body.Add("message", content); resp = req.Post(body, encoding); result = resp.ReadAsString(encoding); Console.WriteLine(result); } } public static class HttpWebRequestExtension { public static HttpWebResponse Get(this HttpWebRequest req) { req.Method = "GET"; req.UserAgent = "Mozilla/4.0"; return req.GetResponse() as HttpWebResponse; } public static HttpWebResponse Post(this HttpWebRequest req, Dictionary<string, string> body, Encoding encoding) { req.Method = "POST"; req.UserAgent = "Mozilla/4.0"; req.ContentType = "application/x-www-form-urlencoded"; var data = ""; foreach (var kv in body) data += string.Format("{0}={1}&", kv.Key, HttpUtility.UrlEncode(kv.Value, encoding)); data = data.TrimEnd('&'); var bytData = System.Text.Encoding.ASCII.GetBytes(data); req.ContentLength = bytData.Length; using (var rs = req.GetRequestStream()) rs.Write(bytData, 0, bytData.Length); return req.GetResponse() as HttpWebResponse; } } public static class HttpWebResponseExtension { public static string ReadAsString(this HttpWebResponse resp, Encoding encoding) { using (resp) { using (var rs = resp.GetResponseStream()) { using (var sr = new StreamReader(rs, encoding)) return sr.ReadToEnd(); } } } }} 自定义时间控件默认显示当前时间 tabcontrol 下的tabitem 怎么拿到他是否存在 移动设备的COM端口号怎么获得 晚上了,问个小问题 C# 强悍的问题.谁遇到过? 多线程操作数据库与短信群发问题 程序里任务计划的修改功能 急!! 请问,这个解决方案如何打包 可不可以将DLL文件反编译成C#文件,如果行的话应该怎么办?? 字符串转换16进制数组时报的错,求解 大家进来看下要怎样修改??
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*
Referer: http://bbs.egou.com/viewthread.php?tid=1222671&extra=page%3D1
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS86335; .NET4.0C; TheWorld)Content-Type:application/x-www-form-urlencoded
Content-Type: application/x-www-form-urlencoded
Host: bbs.egou.com
Cookie: cfT_sid=d3tEE3; cfT_auth=3e16Nlcetojn1rORkfdf2fmAYKaYum%2FncGsg15xl%2BxdK6R3qNp3%2FGvAlTryIOkitXa%2F5IyffUlfGaMnk8D%2FBgY%2FBL9GhYw; egouUser=ZWdvb2dsZS8vZmEzNjFiOGZlNjVhMDM4NmY5NDBjYTE4Yjk2MGNhODMvLzEwNDQyMTAvL0AvLzhkMg%3D%3D; cfT_oldtopics=D1222290D
Content-Length: 99
Expect: 100-continue这是我用网站回复帖子时用WrieShark抓的包POST /post.php?action=reply&fid=19&tid=1222671&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1 HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*
Referer: http://bbs.egou.com/viewthread.php?tid=1222671&extra=page%3D1
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS86335; .NET4.0C; TheWorld)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: bbs.egou.com
Content-Length: 68
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: __utma=18130278.2129965114.1309415671.1309479323.1309483287.9; __utmz=18130278.1309483287.9.9.utmcsr=bbs.egou.com|utmccn=(referral)|utmcmd=referral|utmcct=/forumdisplay.php; _adwp=131476444.3955851719.1309415675.1309479324.1309483289.9; Hm_lvt_977ed62600436aca38f88304752ba8c4=1309483289342; _adwt=1309483289; smile=5D1; cfT_sid=0t5K0k; __utmb=18130278.15.10.1309483287; _adwb=131476444; cfT_oldtopics=D1222671D; cfT_fid19=1309482480; __utmc=18130278; t=undefined; Hm_lpvt_977ed62600436aca38f88304752ba8c4=1309483410498; _adwc=131476444%23http%253A%252F%252Fbbs.egou.com%252Fforumdisplay.php%253Ffid%253D19%2526page%253D1; cfT_auth=ba82oK4WDznENUDW9CTAMVV9%2Bwq%2FPKKtXLPZXwezTq2pdiotiONvChbVco1c6CpkMpXapH%2BhyGuiwWqNIiXBj0plRSiJMQ; egouUser=ZWdvb2dsZS8vZmEzNjFiOGZlNjVhMDM4NmY5NDBjYTE4Yjk2MGNhODMvLzEwNDQyMTAvL0AvLzhkMg%3D%3D
2.Cookie还少
可是cookies就不明白为什么这样了。请指教。
public string formhash;
public CookieContainer cookieContainer;
}
WebInfo GetWebInfo(string url){
WebInfo webInfo=new MainForm.WebInfo();
HttpWebRequest webRequest=(HttpWebRequest)WebRequest.Create(url);
webRequest.Method="GET";
CookieContainer cookieC=new CookieContainer();
cookieC.GetCookies(webRequest.RequestUri);
webInfo.cookieContainer=cookieC;
HttpWebResponse webResponse=(HttpWebResponse)webRequest.GetResponse();
Stream responseStream=webResponse.GetResponseStream();
StreamReader responseReader=new StreamReader(responseStream,Encoding.GetEncoding("gb2312"));
string str=responseReader.ReadToEnd();
string formhash=str.Substring(str.IndexOf("formhash=")+9,8);
webInfo.formhash=formhash;
return webInfo;
}
void PostMessage(string text){
HttpWebRequest webRequest=WebRequest.Create("http://bbs.egou.com/viewthread.php?tid=1222290&extra=page%3D1") as HttpWebRequest;
webRequest.Method="GET";
webRequest.CookieContainer=webInof.cookieContainer;
HttpWebResponse tmpResponse=webRequest.GetResponse() as HttpWebResponse;
Stream tmpStream=tmpResponse.GetResponseStream();
StreamReader tmpReader=new StreamReader(tmpStream,Encoding.GetEncoding("gb2312"));
string referer=webRequest.GetResponse().ResponseUri.AbsoluteUri;
webRequest=WebRequest.Create("http://bbs.egou.com//post.php?action=reply&fid=19&tid=1222671&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1") as HttpWebRequest;
webRequest.Method="POST";
webRequest.Accept="image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*";
webRequest.Referer="http://bbs.egou.com/viewthread.php?tid=1222671&extra=page%3D1";
webRequest.UserAgent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS86335; .NET4.0C; TheWorld)Content-Type:application/x-www-form-urlencoded";
webRequest.ContentType="application/x-www-form-urlencoded";
webRequest.CookieContainer=webInof.cookieContainer;
string pst="formhash="+webInof.formhash+"&subject=&usesig=1&message=%21%21%21%21%21%21%21%21%21%21%21%21%21%21%21%21%21";
byte[] b=Encoding.ASCII.GetBytes(pst);
Stream requestStrem=webRequest.GetRequestStream();
requestStrem.Write(b,0,b.Length);
requestStrem.Close();
HttpWebResponse webResponse=webRequest.GetResponse() as HttpWebResponse;
Stream responseStream=webResponse.GetResponseStream();
StreamReader responseReader=new StreamReader(responseStream,Encoding.GetEncoding("gb2312"));
richTextBox1.Clear();
richTextBox1.AppendText(responseReader.ReadToEnd());
}
WebInfo webInof;
void LoginBtnClick(object sender, EventArgs e)
{
webInof=GetWebInfo("http://bbs.egou.com/logging.php?action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=floatwin_login");
Encoding coder=Encoding.GetEncoding("utf-8");
string str="formhash="+webInof.formhash+"&loginfield=username&username="+
userText.Text+"&password="+passText.Text+
"&questionid=0&answer=&loginsubmit=true";
byte[] postByte=Encoding.ASCII.GetBytes(str);
HttpWebRequest hwq=(HttpWebRequest)WebRequest.Create("http://bbs.egou.com/logging.php?action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=floatwin_login");
hwq.CookieContainer=webInof.cookieContainer;
hwq.Method="POST";
hwq.ContentType="application/x-www-form-urlencoded";
hwq.Accept="application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
hwq.UserAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10";
hwq.Referer="http://bbs.egou.com/logging.php?action=login";
hwq.KeepAlive=true;
hwq.ContentLength=postByte.Length;
Stream postStream=hwq.GetRequestStream();
postStream.Write(postByte,0,postByte.Length);
postStream.Close();
HttpWebResponse wrp=(HttpWebResponse)hwq.GetResponse();
Stream receiveStream=wrp.GetResponseStream();
StreamReader reader=new StreamReader(receiveStream,Encoding.GetEncoding("gb2312"));
string s=reader.ReadToEnd();
receiveStream.Close();
richTextBox1.Clear();
richTextBox1.AppendText(s);
PostMessage("xx");
}
if(typeof messagehandle_fastpost =='function') {messagehandle_fastpost('HALTED', '对不起,本版块只有特定用户组可以回复。');}
</script>]]></root>我搞了下,提示这个,可能是没有该版块回复的权限..
2:其实可以通过TcpClient直接写入,你抓取到的http头文件。
request->Credentials = myCredentials;
request->PreAuthenticate = true;//必須要1:确定一下有没有 账号密码 然后做如上设置
2:实在不行 就有TcpClient 直接写入http头文件吧
我私信一个帐号与您好哦。
的。你拷贝到 Console 工程里就能运行。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;namespace HttpWebRequest2
{
class Program
{
static void Main(string[] args)
{
Login("饿狗传说", "19850420");
var reply = "thanks by JustACoder " + DateTime.Now.ToString("yyyyMMdd HHmmss");
PostReply("1202032", reply);
Console.Read();
} private static CookieContainer _cookie = new CookieContainer(); static string GetFormHash(string content)
{
Regex regex = new Regex("name=\"formhash\" value=\"(.*?)\"");
return regex.Match(content).Groups[1].Value;
} static void Login(string username, string passwd)
{
var encoding = Encoding.GetEncoding("GBK");
var url = "http://bbs.egou.com/logging.php?action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=floatwin_login";
var req = WebRequest.Create(url) as HttpWebRequest;
req.CookieContainer = _cookie;
var resp = req.Get();
var result = resp.ReadAsString(encoding);
var hash = GetFormHash(result);
Console.WriteLine(hash); var body = new Dictionary<string, string>();
body.Add("loginfield", "username");
body.Add("username", username);
body.Add("password", passwd);
body.Add("formhash", hash);
body.Add("questionid", "0");
body.Add("answer", "");
body.Add("loginsubmit", "true"); req = WebRequest.Create(url) as HttpWebRequest;
req.CookieContainer = _cookie;
req.Referer = "http://bbs.egou.com/logging.php?action=login";
resp = req.Post(body, encoding);
result = resp.ReadAsString(encoding);
Console.WriteLine(result);
} static void PostReply(string tid, string content)
{
var encoding = Encoding.GetEncoding("GBK");
var url = "http://bbs.egou.com/viewthread.php?tid={0}&extra=&highlight=&page=1";
url = string.Format(url, tid);
var req = WebRequest.Create(url) as HttpWebRequest;
req.CookieContainer = _cookie;
var resp = req.Get();
var result = resp.ReadAsString(encoding);
var hash = GetFormHash(result);
Console.WriteLine(hash); url = "http://bbs.egou.com/post.php?action=reply&fid=168&tid={0}&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1";
url = string.Format(url, tid);
req = WebRequest.Create(url) as HttpWebRequest;
req.CookieContainer = _cookie;
var body = new Dictionary<string, string>();
body.Add("formhash", hash);
body.Add("subject", "");
body.Add("usesig", "0");
body.Add("message", content); resp = req.Post(body, encoding);
result = resp.ReadAsString(encoding);
Console.WriteLine(result);
}
} public static class HttpWebRequestExtension
{
public static HttpWebResponse Get(this HttpWebRequest req)
{
req.Method = "GET";
req.UserAgent = "Mozilla/4.0";
return req.GetResponse() as HttpWebResponse;
} public static HttpWebResponse Post(this HttpWebRequest req, Dictionary<string, string> body, Encoding encoding)
{
req.Method = "POST";
req.UserAgent = "Mozilla/4.0";
req.ContentType = "application/x-www-form-urlencoded";
var data = "";
foreach (var kv in body)
data += string.Format("{0}={1}&", kv.Key, HttpUtility.UrlEncode(kv.Value, encoding));
data = data.TrimEnd('&');
var bytData = System.Text.Encoding.ASCII.GetBytes(data);
req.ContentLength = bytData.Length;
using (var rs = req.GetRequestStream())
rs.Write(bytData, 0, bytData.Length);
return req.GetResponse() as HttpWebResponse;
}
} public static class HttpWebResponseExtension
{
public static string ReadAsString(this HttpWebResponse resp, Encoding encoding)
{
using (resp)
{
using (var rs = resp.GetResponseStream())
{
using (var sr = new StreamReader(rs, encoding))
return sr.ReadToEnd();
}
}
}
}
}