首页 新闻 会员 周边 捐助

关于C#写的自动发帖机的程序,发帖总是失败。

0
悬赏园豆:100 [已解决问题] 解决于 2012-08-29 17:41

//我写的这个小程序没有什么恶意攻击网站的意思,就是老师布置的一个小作业,自己假期在学校做了挺长时间了登录可以成功,发帖就一直不成功。可是发帖我真的不知道哪里错了,也没有改对过,这个问题已经快两周了。我真心的请教,希望哪位会的话可以帮我看一下程序,真的谢谢你们了。

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel; using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Net; using System.IO;

using System.Web;

namespace WindowsFormsApplication3

{    

public partial class Form1 : Form     {      

   static string BBSURL = "http://bbs.xdnice.com";//好网       

  static string USERNAME = "xxxxx";      

   static string PASSWORD = "xxxxx";

     private CookieCollection gCookieCollention = null;       

   private HttpWebRequest BBSRequest = null;       

   private HttpWebResponse BBSResponse = null;             

   public Form1()        

{             InitializeComponent();         }

             

public void SendLogin()        

{            

string postdata = string.Format("formhash=061daa01&referer=forum.php&loginfield=username&username={0}&password={1}&questionid=0&answer=", USERNAME, PASSWORD);             postdata = EncodePost(postdata);           

 byte[] byteArray = Encoding.Default.GetBytes(postdata);            

string loginUrl = string.Format("{0}/member.php?mod=logging&action=login&loginsubmit=yes&loginhash=LjV5V&inajax=1 ", BBSURL);            

try            

{    BBSRequest = (HttpWebRequest)WebRequest.Create(loginUrl);

    BBSRequest.CookieContainer = new CookieContainer();                                    BBSRequest.ContentType = "application/x-www-form-urlencoded";             

  BBSRequest.Method = "POST";                           

//BBSRequest.Headers["Accept-Language"]="zhCN";               

 //BBSRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; 360SE)";

                     

BBSRequest.ContentLength = byteArray.Length;           

Stream requestStream = BBSRequest.GetRequestStream();                requestStream.Write(byteArray,0,byteArray.Length);                         

requestStream.Close();                            

BBSResponse = (HttpWebResponse)BBSRequest.GetResponse();       

Stream responseStream = BBSResponse.GetResponseStream();             

StreamReader reader = new StreamReader(responseStream,  Encoding.GetEncoding("gbk"));               

string responseHtml = reader.ReadToEnd();           

  reader.Close();                

responseStream.Close();               

  BBSResponse.Close();

   gCookieCollention = BBSResponse.Cookies;

if (responseHtml.IndexOf("欢迎您回来") > 0)                

{                     MessageBox.Show("登录成功");                    

  MessageBox.Show(responseHtml);                 }     

            else               

  {   MessageBox.Show("登录失败");                  

    MessageBox.Show(responseHtml);                  }           

   }            

  catch (Exception ex)            

{                 MessageBox.Show(ex.Message);             }     

   }

 

        /// 发帖       

public void SendInfo()         {                                   

try             {                

string infoUrl = string.Format("{0}/forum.php?mod=post&action=newthread&fid=66&extra=&topicsubmit=yes ", BBSURL);                

BBSRequest = (HttpWebRequest)WebRequest.Create(infoUrl);

BBSRequest.ContentType = "application/x-www-form-urlencoded";                 BBSRequest.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, ";                 BBSRequest.Headers["Accept-Language"] = "zh-CN";                 BBSRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; 360SE)";                

BBSRequest.Method = "POST";//协议方式               

  BBSRequest.Referer = string.Format("{0}/forum.php?mod=post&action=newthread&fid=66&referer=http%3A//bbs.xdnice.com/forum-66-1.html", BBSURL);                               

BBSRequest.KeepAlive = true;//与internet建立持久连接                 BBSRequest.AllowWriteStreamBuffering = false;BBSRequest.CookieContainer = new CookieContainer();                

BBSRequest.CookieContainer.Add(gCookieCollention);

 string topic = string.Format("formhash=4899073d&posttime=1343703802&wysiwyg=1&typeid=0&subject=hello+everyone&message=this+is+my+first+.%0D0A&replycredit_extcredits=0&replycredit_times=1&replycredit_membertimes=1&replycredit_random=100&price=&tags=everyone%2Chello&save=&uploadalbum=-2&newalbum=%C7%EB%CA%E4%C8%EB%CF%E0%B2%E1%C3%FB%B3%C6&allownoticeauthor=1");                

topic = EncodePost(topic);                 //post开始                   

byte[] byteArray = Encoding.Default.GetBytes(topic);                 BBSRequest.ContentLength = byteArray.Length;//请求内容长度               

Stream postStream = BBSRequest.GetRequestStream();//创建请求流                 postStream.Write(byteArray,0,byteArray.Length);// 请求数据放入请求流                 postStream.Close();

BBSResponse = (HttpWebResponse)BBSRequest.GetResponse();

Stream newStream = BBSResponse.GetResponseStream();

  StreamReader reader = new StreamReader(newStream,Encoding.GetEncoding("gbk"));                

string responseHtml = reader.ReadToEnd(); //读取响应流                

reader.Close();               

  newStream.Close();                

BBSResponse.Close();

   if (responseHtml.IndexOf("学习交流") > 0)//提示信息                

{                     MessageBox.Show("发帖成功");                    

                    MessageBox.Show(responseHtml);                 }                

else                

{                     MessageBox.Show("发帖失败");                    

                    MessageBox.Show(responseHtml);                 }            

}            

catch (Exception ex)          

   {                 MessageBox.Show(ex.Message);                           }        

  }

      

   private string EncodePost(string input)        

{            

string output = null;           

Char[] reserved = { '?', '=', '&' };            

if (input != null)            

{                 int i = 0, j;                

while (i < input.Length)                 {                    

j = input.IndexOfAny(reserved, i);                    

if (j == -1)                    

{                        

output = output + HttpUtility.UrlEncode(input.Substring(i, input.Length - i), System.Text.Encoding.GetEncoding("gbk"));                        

break;                     }                    

string tt = HttpUtility.UrlEncode(input.Substring(i, j - i), System.Text.Encoding.GetEncoding("gbk"));                    

output += tt;                    

output += input.Substring(j, 1);                    

i = j + 1;                 }                

return output;             }           

  else                 return null;         }

 

   private static void RemoveCookies()      

   {             int cookiesmax = Environment.GetFolderPath

(Environment.SpecialFolder.Cookies).Length;            

for (int i = 0; i < cookiesmax; i++)                

Environment.GetFolderPath(Environment.SpecialFolder.Cookies).Remove(0);

        }        

   private void Form1_Load(object sender, EventArgs e)        

{

        }

        private void button1_Click(object sender, EventArgs e)       

  {                RemoveCookies();               

                   SendLogin();                  

                  SendInfo();                

                 button1.Enabled = false;

            }            

       

                 }   

}

C#
莞尔求知的主页 莞尔求知 | 初学一级 | 园豆:94
提问于:2012-08-09 21:28
< >
分享
最佳答案
1

看了一下 论坛是用 DiscusZ 做的, 使用了 formhash 做验证。 你不能使用同样的formhash 发新帖, 这样通不过验证。

在发帖之前需要 访问发新帖的page - 比如 forum.php?mod=post&action=newthread&fid=66  得到的page中 会有

<input type="hidden" name="formhash" id="formhash" value="e26b3058" />
<input type="hidden" name="posttime" id="posttime" value="1344559086" />
<input type="hidden" name="wysiwyg" id="e_mode" value="1" />

取到 formhash 和posttime的值  然后带入你的sendinfo方法 这样才能发帖。

收获园豆:100
gunsmoke | 老鸟四级 |园豆:3592 | 2012-08-10 08:48
其他回答(1)
0

学习一下

王根慧 | 园豆:202 (菜鸟二级) | 2013-03-21 14:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册