zoukankan      html  css  js  c++  java
  • 自动登录DISCUZ,发帖的代码(部分)

    class Robot
        {
            /// attributes
            // cookies
            private CookieCollection CkCollection = null;
            // request and response
            private HttpWebRequest SparkRequest = null;
            private HttpWebResponse SparkResponse = null;
            // some url
            private string LoginUrl = null;
            private string ReplyUrl = null;
            // constructer
            public Robot()
            {
                CkCollection = new CookieCollection();
            }
            // logining
            public string Login(string url, string usr,string pass)
            {
                string Return = null;
                this.LoginUrl = url;
                // may be I should add a functin for create string
                string loginstr = "formhash=3bd8bc0a&referer=index.php&loginmode=&styleid=&cookietime=2592000&loginfield=username&username=" + usr;
                loginstr += "&password=" + pass;
                loginstr += "&questionid=0&answer=&loginsubmit=提 交";
                loginstr = EncodePost(loginstr);
                byte[] replybyte = Encoding.UTF8.GetBytes(loginstr);
                
                try
                {
                    CookieContainer sparkc = new CookieContainer();
                    SparkRequest = (HttpWebRequest)WebRequest.Create(url);
                    SparkRequest.CookieContainer = sparkc;
                    SparkRequest.ContentType = "application/x-www-form-urlencoded";
                    SparkRequest.Method = "POST";
                    SparkRequest.ContentLength = replybyte.Length;
                    Stream newStream = SparkRequest.GetRequestStream();
                    newStream.Write(replybyte, 0, replybyte.Length);
                    newStream.Close();
                    SparkResponse = (HttpWebResponse)SparkRequest.GetResponse();
                    Stream dataStream = SparkResponse.GetResponseStream();
                    StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gb2312"));
                    Return = reader.ReadToEnd();
                    // check cookie
                    foreach (Cookie temp in SparkResponse.Cookies)
                    {
                        if (temp.Domain != "spark.cjlu.edu.cn")
                            temp.Domain = "spark.cjlu.edu.cn";
                    }
                    CkCollection = SparkResponse.Cookies;
                }
                catch
                {
                    return null;
                }
                return Return;
            }
            // overload
            /*
            public bool Login(string usr, string pass)
            {
                ;
            }*/
            // reply……
            public string Reply(string url,string formhash,string title,string content)
            {
                SparkRequest = (HttpWebRequest)WebRequest.Create("http://spark.cjlu.edu.cn/bbs/"+url);
                SparkRequest.ContentType = "application/x-www-form-urlencoded";
                SparkRequest.Method = "POST";
                //SparkRequest.Referer = "http://spark.cjlu.edu.cn/bbs/index.php";
                SparkRequest.KeepAlive = true;
                SparkRequest.AllowWriteStreamBuffering = false;
                // set cookie
                CookieContainer cookieCon = new CookieContainer();
                SparkRequest.CookieContainer = cookieCon;
                SparkRequest.CookieContainer.Add(CkCollection);
                // get post value
                string reply = EncodePost("formhash=" + formhash + "&subject=&usesig=1&message=" + content);
                byte[] replybyte = Encoding.UTF8.GetBytes(reply);
                SparkRequest.ContentLength = replybyte.Length;
                Stream newStream = SparkRequest.GetRequestStream();
                newStream.Write(replybyte, 0, replybyte.Length);
                newStream.Close();
                // get response
                SparkResponse = (HttpWebResponse)SparkRequest.GetResponse();
                Stream dataStream = SparkResponse.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gb2312"));
                string tt = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
                SparkResponse.Close();
                return tt;
            }
            // encode the post string
            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("gb2312"));
                            break;
                        }
                        string tt = HttpUtility.UrlEncode(input.Substring(i, j - i), System.Text.Encoding.GetEncoding("gb2312"));
                        output += tt;
                        output += input.Substring(j, 1);
                        i = j + 1;
                    }
                    return output;
                }
                else
                    return null;
            }
        } 
  • 相关阅读:
    神奇的flex布局
    reset、revert、rebase
    Vue.filter过滤器
    moment.js时间格式化总结
    Vue之组件大全
    过滤器filter
    Vue之animate
    Vue之axios
    Mac OS系统上测试PHP代码前的准备工作 | 使用XAMPP搭建Apache服务器的步骤
    Python中的标识符、关键字、变量、语句、注释、模块
  • 原文地址:https://www.cnblogs.com/top5/p/2556197.html
Copyright © 2011-2022 走看看