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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 20843|回复: 13

[源码分享] 学习三天POST登陆DZ论坛源码-老鸟勿笑

[复制链接]
发表于 2013-8-1 00:00:57 | 显示全部楼层 |阅读模式


BY:受人之渔,还人之鱼!           

重要提示: “ 请把论坛的主题帖编辑打开” 和在快捷导航加入进入收藏帖子链接    {:soso_e113:}

首先感谢 站长  苏飞    站长是很辛苦的!没有收入,穷光蛋一个,就捧个人场吧!

刚学习不到一礼拜,根据自己的逻辑搞了登陆以及发帖,当然在我看了http://www.sufeinet.com/thread-3830-1-1.html 这位大虾米的帖子之后,才发现我做的是多么...,当然我知足。本来不想发源码了,后来想了想就当让和我一样的小小白学习下经验也好。

简单说下这几天的学习经历吧。
第一天当然是无意中进入这论坛开始    第一天发的帖子 http://www.sufeinet.com/thread-4404-1-1.html
我什么也不懂,看不懂英语,方法也学的不好,也就意味着感觉 站长的类很乱...基本是瞎逛了

第二天开始正式研究登陆,抓包 分析 ....研究 登陆    第二天      http://www.sufeinet.com/thread-4417-1-1.html

第三天 发现了助手可以生成代码 ,于是乎....    http://www.sufeinet.com/thread-4427-1-1.html

第四天  为了一个  formhash 研究了8- 10小时 未果  临了想到了 winform  于是乎把  form 和hash 分开用 灵格斯翻译了下..然后百度了下..
http://www.sufeinet.com/thread-4431-1-1.html
http://www.sufeinet.com/thread-4445-1-1.html

第五天 试着做了  且 实现了  登陆之后可以发帖了 ...


今天重写了下代码   代码在下边发上来  大概流程就是这样了 希望能帮助刚学习的新手朋友

.明天研究下 这货发的源码http://www.sufeinet.com/thread-3830-1-1.html  或去找下贴吧的易语言源码,研究下百度贴吧.的登陆...


再次感谢 苏飞站长 给予的帮助 !!!


源码 附上 论坛登录发帖.rar (82.64 KB, 下载次数: 1018)

本帖被以下淘专辑推荐:



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2013-8-1 00:01:58 | 显示全部楼层
@站长苏飞
[code=csharp]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;

namespace 论坛登录发帖
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /*
         *
         * 因刚学没几天,技术有限,一些功能还未实现
         *
         * 如果测试不成功,请重新抓包, 只需要  cookie的 WuXn_2132_saltkey和WuXn_2132_auth
         *
         *  各处url可能也需要更改,还有post的一些数据
         *
         * 测试本地论坛可以成功,但是换论坛之后出现cookie不能抓取 原因WuXn_2132_saltkey 前边的WuXn会发生改变不知道原因
         *
         * post的数据串是否会改变未测试,验证码部分因为找不到验证码地址没有添加,正在研究中
         *
         * 一些细节问题还需要处理,总体登陆流程大概就是这样 应该是...
         *
         *
         * BY:受人之渔,还人之鱼!
         *
         * 论坛账号 QQ 970443232  一起交流!
         *
         */



        //cookie的值方便调用
        string cookie = "";
        //发帖时所需要的formhash值
        string strFormHash = "";
        private void btnLogin_Click(object sender, EventArgs e)
        {
            
            //上边工作都完成以后 这里就简单了 这里拼接连接字符串
            string url = txtUrl.Text.Trim() + "member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1";
            //有些参数可以用户设置,这里只是练习使用方法
            string[] str = PostGetHtml(url, txtCoding.Text.Trim(), "post", "",
                "fastloginfield=username&username=" + txtUserName.Text.Trim() + "&password=" + txtPassWord.Text.Trim() + "&quickforward=yes&handlekey=ls",
                "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
                "application/x-www-form-urlencoded");
            //取出cookie的值
            
            cookie = "WuXn_2132_saltkey=" + HttpCookieHelper.GetCookieValue("WuXn_2132_saltkey", str[1]) +
                "; WuXn_2132_auth=" + HttpCookieHelper.GetCookieValue("WuXn_2132_auth", str[1]);
            //取出html判断登陆是否成功
            string html = str[0];
            if (Regex.IsMatch(html, @"<root>"))
            {
                gbLogin.Text = "账号登录----登陆成功";
            }
            else
            {
                gbLogin.Text = "账号登录----登陆失败";
            }

            //get一次 取出formhash  登陆之后的代码没有
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                // 这里代码还可以精简 url使用的是个人短消息页面
                URL = txtUrl.Text.Trim() + "home.php?mod=space&do=pm",//URL     必需项   
                Encoding = System.Text.Encoding.GetEncoding(txtCoding.Text.Trim()),//URL     可选项 默认为Get   
                Method = "get",//URL     可选项 默认为Get     
                Cookie = cookie,//字符串Cookie     可选项   
                Timeout = 100000,//连接超时时间     可选项默认为100000   
                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
            };
            HttpResult result = http.GetHtml(item);
            //得到html
            string strhtml = result.Html;
            //正则提取 formhash 值   这段代码是网上复制的 可以调试看过程
            Regex reg = new Regex("out&formhash=(.*)\"");//out&formhash=(.*)\">"   (.*)中的值是需要的
            MatchCollection mts = reg.Matches(strhtml);
            foreach (Match itemformhash in mts)
            {
                //this.txtID.Text += item.Value.Replace("\"", "");
                string lines = itemformhash.Value.Replace("\"", "");
                string[] strs = lines.Split('=');
                strFormHash = strs[1];

            }

        }


        private void btnFaTie_Click(object sender, EventArgs e)
        {
            //上边工作都完成以后 这里就简单了 这里拼接连接字符串
            string url = txtUrl.Text.Trim()+"forum.php?mod=post&action=newthread&fid="+txtId.Text.Trim()+"&extra=&topicsubmit=yes";
            //拼接Post内容
            string strPost = "formhash=" + strFormHash + "&posttime=1375173832&wysiwyg=1&subject=" + txtBiaoTi.Text + "&message=" + txtNeiRong.Text + "&save=&usesig=1&allownoticeauthor=1";
            //使用方法进行操作
            PostGetHtml(url, txtCoding.Text.Trim(), "post", cookie,strPost,
                "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
                "application/x-www-form-urlencoded");

            //发帖完毕 get发帖板块 查找是否发帖成功
            //拼接url地址
              url =txtUrl.Text.Trim()+"forum.php?mod=forumdisplay&fid="+txtId.Text.Trim();
              string strHtml= GetHtml(url, txtCoding.Text.Trim(), cookie);
              if (Regex.IsMatch(strHtml, txtBiaoTi.Text.Trim()))
              {
                  label7.Text = "发布成功";
              }
              else
              {
                  label7.Text = "发布失败";
              }
            
        }

        /// <summary>
        /// post get 方法 string数组[0]=thml,[1]=cookie
        /// </summary>
        /// <param name="url">传入一个url</param>
        /// <param name="encoding">传入的编码</param>
        /// <param name="method"> post或get方式</param>
        /// <param name="cookie">传入的cookie值 可选登陆可以传入空</param>
        /// <param name="postData">发送的数据</param>
        /// <param name="userAgent">浏览器 版本 操作系统 </param>
        /// <param name="contenType">返回的类型</param>
        /// <returns></returns>
        private static string[] PostGetHtml(string url, string encoding, string method, string cookie,
            string postData, string userAgent, string contenType)
        {

            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                //根据编程助手生成代码,编写一个方法,方便调用 减少代码量
                URL = url,//Url 必须
                Encoding = System.Text.Encoding.GetEncoding(encoding), // 数据传输编码 默认gbk 应该是未测试
                Method = method,// 数据方式,可选项默认Get
                Cookie = cookie,//字符串cookie 可选
                Postdata = postData,//Post数据
                //考虑到网络问题这里添加连接和Post写入事件 --
                Timeout = 100000,//连接超时时间     可选项默认为100000   
                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = userAgent,//用户的浏览器类型,版本,操作系统 可选项有默认值  默认值未查看
                ContentType = contenType,//返回类型    可选项有默认值 默认值未查看
            };
            HttpResult result = http.GetHtml(item);
            //根据传入cookie是否有值,返回html或cookie加html(问题 如果随便传入一个cookie值,将会造成登陆失败,也会
            //返回两个失败值)
            if (cookie == "")
            {
                //如果是登陆则返回html和cookie方便使用
                string[] strHtmlCookie = { result.Html, result.Cookie };
                return strHtmlCookie;
            }
            else
            {
                // 如果不是登陆则返回html
                string[] strHtmlCookie = { result.Html };
                return strHtmlCookie;
            }

        }

        private static string GetHtml(string url,string encoding, string cookie)
        {
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                // 这里代码还可以精简 url使用的是个人短消息页面
                URL = url,//URL     必需项   
                Encoding = System.Text.Encoding.GetEncoding(encoding),//URL     可选项 默认为Get   
                Method = "get",//URL     可选项 默认为Get     
                Cookie = cookie,//字符串Cookie     可选项   
                Timeout = 100000,//连接超时时间     可选项默认为100000   
                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   
            };
            HttpResult result = http.GetHtml(item);
            //得到html
            return result.Html;
        }

    }
}
[/code]
发表于 2013-8-1 08:03:20 | 显示全部楼层
恩这个不错,已收录到HttpHelper例子
发表于 2014-5-27 10:07:04 | 显示全部楼层
膜拜中……
回复

使用道具 举报

发表于 2014-6-5 13:41:52 | 显示全部楼层
楼主天才啊,自愧不如啊
发表于 2014-6-9 14:45:28 | 显示全部楼层
我今天才开始研究这个。但是我测试的是自己的网站,登录是异步的。post始终没登录上啊。用的是那个开发助手。然后还有就是,访问网站主页获取的cookie也不对,没有,是空的。
 楼主| 发表于 2014-6-11 13:11:46 | 显示全部楼层
lx99959 发表于 2014-6-9 14:45
我今天才开始研究这个。但是我测试的是自己的网站,登录是异步的。post始终没登录上啊。用的是那个开发助手 ...

不知道你是用的什么程序,不过一般网站或开源程序,仔细研究下抓包和登陆需要的cookie一般都是没问题的
发表于 2014-10-24 13:59:10 | 显示全部楼层
强烈支持楼主ing……
发表于 2014-10-26 22:00:46 | 显示全部楼层
楼主 有没有POST系列的教程啊
发表于 2014-11-28 02:12:00 | 显示全部楼层
不错,有注释我下载来看看
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-4-24 01:18

© 2014-2021

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