首页 新闻 会员 周边 捐助

C#写的自动发贴机。发帖时返回 您的请求来路不正确,无法提交

-1
悬赏园豆:80 [已解决问题] 解决于 2010-06-15 12:30

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 WindowsFormsApplication1
{


    public partial class Form1 : Form
    { private readonly string BBSURL = "http://bbs.admin5.com";
        private readonly string USERNAME = "用户名";
        private readonly string PASSWORD = "密码";

        private CookieCollection gCookieCollention = null;
        private HttpWebRequest BBSRequest = null;
        private HttpWebResponse BBSResponse = null;
        public Form1()
        {
            InitializeComponent();
        }
         private void startBtn_Click(object sender, EventArgs e)
        {
            string loginUrl = string.Format("{0}/logging.php?action=login&loginsubmit=yes&floatlogin=yes&inajax=1 ", BBSURL);
            RemoveCookies();
            MaliciousLogin(loginUrl, USERNAME, PASSWORD);
            startBtn.Enabled = false;
        }
        /// <summary>
        /// 自动登录
        /// </summary>
        public void MaliciousLogin(string loginUrl, string usr, string pwd)
        {
            string responseHTML = string.Empty; ;
            string loginstr = string.Format("formhash=270dff9b&referer=http%3A%2F%2Fbbs.admin5.com%2F&loginfield=username&username={0}&password={1}&questionid=0&answer=", usr, pwd);
            loginstr = EncodePost(loginstr);
            byte[] replybyte = Encoding.UTF8.GetBytes(loginstr);

            try
            {
                CookieContainer _cookieContainer = new CookieContainer();
                BBSRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
                BBSRequest.CookieContainer = _cookieContainer;
                BBSRequest.ContentType = "application/x-www-form-urlencoded";
           
                BBSRequest.Method = "POST";
                //post 开始
                BBSRequest.ContentLength = replybyte.Length;
                Stream newStream = BBSRequest.GetRequestStream();
                newStream.Write(replybyte, 0, replybyte.Length);
                newStream.Close();
                //post 结束

                //返回HTML
                BBSResponse = (HttpWebResponse)BBSRequest.GetResponse();
                Stream dataStream = BBSResponse.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gbk"));
                responseHTML = reader.ReadToEnd();

 

                gCookieCollention = BBSResponse.Cookies;
                if (responseHTML.IndexOf("登录成功") > 0)
                    MessageBox.Show("Login successful");
                else
                    MessageBox.Show(responseHTML);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


        }
        /// <summary>
        /// post 帖子
        /// </summary>
        private void PostTopic(string forumid, string title, string content)
        {

            try
            {
                BBSRequest = (HttpWebRequest)WebRequest.Create(string.Format("{0}/post.php?action=newthread&fid=204&extra=&topicsubmit=yes", BBSURL, forumid));
 
                BBSRequest.Method = "POST";
               BBSRequest.Referer = "http://bbs.admin5.com/post.php?action=newthread&fid=204&referer=http%3A//bbs.admin5.com/forum-204-1.html" ;
                BBSRequest.KeepAlive = true;
                BBSRequest.AllowWriteStreamBuffering = false;
             
         
          CookieContainer cookieCon = new CookieContainer();
     BBSRequest.CookieContainer = cookieCon;
         BBSRequest.CookieContainer.Add(gCookieCollention);
         BBSRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)";
         BBSRequest.KeepAlive = true;
         BBSRequest.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
        
         string topicStr = "formhash=a3ebd679&posttime=1275473499&wysiwyg=1&iconid=&subject=" + HttpUtility.UrlEncode(title, Encoding.GetEncoding("GBK")) + "&message=" + HttpUtility.UrlEncode(content, Encoding.GetEncoding("GBK")) + "&attention_add=1&usesig=1";  //BuildPostContent(title, content);

                // string topic = EncodePost(topicStr);
                string topic =  topicStr ;

                byte[] replybyte = Encoding.ASCII.GetBytes(topic);
                BBSRequest.ContentLength = replybyte.Length;
                Stream newStream = BBSRequest.GetRequestStream();
             
                newStream.Write(replybyte, 0, replybyte.Length);
                newStream.Close();

                // get response
                BBSResponse = (HttpWebResponse)BBSRequest.GetResponse();
                Stream dataStream = BBSResponse.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gbk"));
                string responseHTML = reader.ReadToEnd();

                reader.Close();
                dataStream.Close();
                BBSResponse.Close();
                if (responseHTML.IndexOf("发表主题成功") > 0)
                    MessageBox.Show("发表主题成功!");
                else
                  textBox1.Text= responseHTML ;
            }
            catch (Exception ex)
            {
              textBox1.Text= ex.ToString();
            }
        }
                private string BoundaryString
        {
            get { return "-----------------------------7d8182810472\r\n"; }
        }
        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 void btnPost_Click(object sender, EventArgs e)
        {
            string forumid = txtForumID.Text.Trim();
            string title = txtTitle.Text.Trim();
            string content =  txtContent.Text.Trim();
            PostTopic(forumid, title, content);
        }
        private void RemoveCookies()
        {
            int cookiesmax = Environment.GetFolderPath(Environment.SpecialFolder.Cookies).Length;
            for (int i = 0; i < cookiesmax; i++)
                Environment.GetFolderPath(Environment.SpecialFolder.Cookies).Remove(0);
        }

 
    
    }
}

 

怎么解决。来路错误是指提交的路径还是那个refere?怎么解决?用fiddler抓取两个路径都没错呀。。。

问题补充: 发帖提示:您的请求来路不正确,无法提交 只是个人学习呀,我又不做坏事。。。
piikee的主页 piikee | 初学一级 | 园豆:160
提问于:2010-06-14 13:27
< >
分享
最佳答案
0

使用http嗅探器,对比一下正确发帖和你模拟的是不是什么地方不同,这个得慢慢的试

收获园豆:80
LittlePeng | 老鸟四级 |园豆:3445 | 2010-06-14 15:27
其他回答(3)
0

太坏了,知道也不跟你说

亚历山大同志 | 园豆:48 (初学一级) | 2010-06-14 14:17
0

这个应该是Referer 没设置好吧?

张少峰 | 园豆:320 (菜鸟二级) | 2010-06-14 17:44
0

在调试模式下看的最清楚,一看就应该知道了。

xhswzx | 园豆:146 (初学一级) | 2010-06-14 22:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册