http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 11082|回复: 11

[例子] discuz论坛发帖

[复制链接]
发表于 2014-11-13 16:17:55 | 显示全部楼层 |阅读模式
[C#] 纯文本查看 复制代码
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 DotNet.Utilities;
using System.Text.RegularExpressions;//添加HttpHelper类的引用

namespace myDiscuz
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string formhash = "";
        
        string cookie = "";
        string saltkey = "";
        string auth = "";
        string encoding = "";
        string domain = "";

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        
       


        /// <summary>
        /// 获得字符串中开始和结束字符串中间得值
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="s">开始</param>
        /// <param name="e">结束</param>
        /// <returns></returns>
        public static string GetValue(string str, string s, string e)
        {
            Regex rg = new Regex("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
            return rg.Match(str).Value;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            domain = textBox3.Text.Trim();
            string url = string.Format("{0}/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1",domain);
            string postData = string.Format("fastloginfield=username&username={0}&cookietime=2592000&password={1}&quickforward=yes&handlekey=ls", txtUser.Text.Trim(), txtPWD.Text.Trim());
            HttpHelper http=new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL=url,
                Method="post",
                ContentType = "application/x-www-form-urlencoded",
                Referer = string.Format("{0}/forum.php", domain),
                Postdata=postData,
                
            };
            HttpResult result = http.GetHtml(item);
            

            Regex reg = new Regex(@"\w{4}_\d{4}_saltkey=\S*;");//out&formhash=(.*)\">"   (.*)中的值是需要的 
            MatchCollection mts = reg.Matches(result.Cookie);
            foreach (Match itemformhash in mts)
            {

                saltkey = itemformhash.Value;
            }

            reg = new Regex(@"\w{4}_\d{4}_auth=\S*;");
            mts = reg.Matches(result.Cookie);
            foreach (Match itemformhash in mts)
            {

                auth = itemformhash.Value;
            }
            cookie = saltkey + auth;

            richTextBox1.Text = cookie;


            if (result.Html.Contains("root"))
            {
                label8.Text = "登陆成功!";
                btnLogin.Enabled = false;
            }
            else
            {
                label8.Text = "登录失败!";
            }

            item = new HttpItem()
            {
                URL = string.Format("{0}/forum.php", domain),
                Cookie = cookie,
            };
            result = http.GetHtml(item);
            encoding = GetValue(result.Html, "charset=", "\"");
            formhash = GetValue(result.Html, "formhash\" value=\"", "\" />").Trim();
            txtFormhash.Text = formhash;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            long time = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
            string posturl = string.Format("{0}/forum.php?mod=post&action=newthread&fid={1}&extra=&topicsubmit=yes",domain, textBox2.Text.Trim());
            string postdata = string.Format("formhash={0}&posttime={1}&wysiwyg=1&typeid={2}&subject={3}&message={4}&replycredit_extcredits=0&replycredit_times=1&replycredit_membertimes=1&replycredit_random=100&readperm=&price=&tags=&rushreplyfrom=&rushreplyto=&rewardfloor=&replylimit=&stopfloor=&creditlimit=&allownoticeauthor=1&usesig=1&save=", formhash, time.ToString(), textBox4.Text.Trim(), textBox1.Text, richTextBox1.Text);
            //string postdata = string.Format("formhash={0}&posttime={1}&wysiwyg=1&typeid={2}&subject={3}&message={4}&replycredit_extcredits=0&replycredit_times=1&replycredit_membertimes=1&replycredit_random=100&rushreplyfrom=&rushreplyto=&rewardfloor=&replylimit=&stopfloor=&creditlimit=&allownoticeauthor=1&addfeed=1&save=&connect_publish_t=0", formhash, time.ToString(), textBox4.Text.Trim(), textBox1.Text, richTextBox1.Text);
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = posturl,
                PostEncoding = System.Text.Encoding.GetEncoding(encoding.Trim()),
                Method="post",
                Postdata = postdata,
                ContentType = "application/x-www-form-urlencoded",
                Cookie = cookie,
                
            };
            HttpResult result = http.GetHtml(item);
            richTextBox1.Text = result.Html;

            item = new HttpItem()
            {
                URL =string.Format("{0}/forum.php?mod=forumdisplay&fid={1}",domain,textBox2.Text.Trim()),
                Cookie = cookie,
            };
            result = http.GetHtml(item);
            if (result.Html.Contains(textBox1.Text.Trim()))
            {
                label7.Text = "发帖成功!";
            }
            else
            {
                label7.Text = "发帖失败!";
                
            }


        }
    }
}


myDiscuz.rar (86.83 KB, 下载次数: 175)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-11-13 20:09:40 | 显示全部楼层
强烈支持楼主ing……
发表于 2014-11-14 14:13:49 | 显示全部楼层
看过帖子回复一下是个好习惯
发表于 2014-11-20 22:08:39 | 显示全部楼层
建议增加一个验证码的功能
发表于 2014-11-22 22:18:39 | 显示全部楼层
非常 鼓劲这上论坛,
发表于 2014-12-14 13:04:51 | 显示全部楼层
强烈支持楼主ing……
要是帖子内容里能添加图片就更美了
发表于 2015-1-4 17:55:06 | 显示全部楼层
怎么没有验证码的判断
发表于 2015-1-7 15:24:49 | 显示全部楼层
学习下
回复

使用道具 举报

发表于 2015-5-18 11:39:05 | 显示全部楼层
好东西,正在学习呢
发表于 2016-3-10 17:03:47 | 显示全部楼层
好文要顶一个 感觉不错
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-22 07:01

© 2014-2021

快速回复 返回顶部 返回列表