• C#模拟登录Twitter 发送私信、艾特用户、回复评论


    这次做成了MVC程序的接口

         private static string UserName = "用户名";
            private static string PassWord = "密码";
            private static string Token = "";
    
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Index(string Receiver, string Msg)
            {
                if (Receiver != "" && Msg != "")
                {
                    CookieContainer RequestCookie = GetCookie(UserName, PassWord);
                    if (RequestCookie != null && Token != "")
                    {
                        CookieContainer cookie = Login(UserName, PassWord, Token, RequestCookie);
                        if (cookie != null)
                        {
                            SendMsg(Token, cookie, Receiver, Msg);
                        }
                    }
                }
                else
                {
                    Response.Write("<script>alert('收件人和发送内容不能为空')</script>");
                }
                return View();
            }
    
            public CookieContainer GetCookie(string UserName, string PassWord)
            {
                HttpWebRequest request = null;
                HttpWebResponse response = null;
                CookieContainer cookie = null;
                try
                {
                    cookie = new CookieContainer();
                    request = (HttpWebRequest)HttpWebRequest.Create("https://twitter.com/login/");
                    //WebProxy Proxy = new WebProxy("127.0.0.1", 1080);
                    //request.Proxy = Proxy;
                    request.Timeout = 180000;
                    request.Method = "GET";
                    request.Host = "twitter.com";
                    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0";
                    //reqsessionGet.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36";
                    request.AddRange(1024);
                    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Headers["Accept-Language"] = "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3";
                    request.CookieContainer = cookie;
                    request.KeepAlive = true;
    
                    response = (HttpWebResponse)request.GetResponse();
                    StreamReader sr = new StreamReader(response.GetResponseStream());
                    string content = sr.ReadToEnd();
                    sr.Close();
                    response.Close();
                    //获取Token字符串
                    Token = Regex.Match(content, @"<input type=""hidden"" value=""(w+)"" name=""authenticity_token""/>").Groups[1].Value;
                    return cookie;
                }
                catch (Exception)
                {
                    request.Abort();
                    Response.Write("<script>alert('未获取到Token,请检查网络连接')</script>");
                    return cookie;
                }
            }
    
            public CookieContainer Login(string UserName, string PassWord, string Token, CookieContainer cookie)
            {
                string PostStr = "session%5Busername_or_email%5D=" + UserName + "&session%5Bpassword%5D=" + PassWord + "&authenticity_token=" + Token + "&scribe_log=&redirect_after_login=&authenticity_token=" + Token;
                byte[] Data = Encoding.UTF8.GetBytes(PostStr);
                HttpWebRequest request = null;
                HttpWebResponse response = null;
                try
                {
                    request = (HttpWebRequest)WebRequest.Create("https://twitter.com/sessions");
                    //WebProxy Proxy = new WebProxy("127.0.0.1", 1080);
                    //request.Proxy = Proxy;
                    request.Timeout = 180000;
                    request.Method = "POST";
                    request.Host = "twitter.com";
                    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36";
                    //reqsession.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36";
                    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Headers["Accept-Language"] = "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3";
                    request.Referer = "https://twitter.com/";
                    request.CookieContainer = cookie;
                    request.KeepAlive = true;
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = Data.Length;
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(Data, 0, Data.Length);
                    requestStream.Close();
    
                    response = (HttpWebResponse)request.GetResponse();
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    string content = sr.ReadToEnd();
                    sr.Close();
                    response.Close();
                    if (content.Contains("查看个人资料"))
                    {
                        Response.Write("登录成功");
                    }
                    else
                    {
                        Response.Write("登录失败");
                    }
                    return cookie;
                }
                catch (Exception)
                {
                    request.Abort();
                    response.Close();
                    Response.Write("<script>alert('登录失败,请检查网络连接')</script>");
                    return cookie;
                }
            }
    
            public void SendMsg(string Token, CookieContainer cookie, string Receiver, string Msg)//发送私信
            {
                string PostStr = "authenticity_token=" + Token + "&screen_name%5B%5D=" + Receiver + "&scribeContext%5Bcomponent%5D=tweet_box_dm&tagged_users=&text=" + HttpUtility.UrlEncode(Msg, Encoding.UTF8) + "&tweetboxId=swift_tweetbox_1472436148667";
                HttpHelper http = new HttpHelper();
                string html = http.GetHtml("https://twitter.com/i/direct_messages/new", cookie, PostStr, true);
                if (html.Contains("该用户没有关注你") || html.Contains("你只可以发送私信给关注你的人"))
                {
                    SendArticle(Token, cookie, Receiver, Msg);
                    Response.Write("<script>alert('您输入的用户名不存在,或该用户没有关注你,已发送推文艾特该用户')</script>");
                }
                else if (html.Contains("无法发送你的私信"))
                {
                    Response.Write("<script>alert('无法发送你的私信')</script>");
                }
                else if (html.Contains(@"u003e"))
                {
                    Response.Write("<script>alert('你给@" + Receiver + "的私信发送成功')</script>");
                }
                else if (html.Contains("404"))
                {
                    Response.Write("<script>alert('服务器未响应,请检查网络连接')</script>");
                }
            }
    
            public ActionResult Article(string Receiver, string Content)
            {
                if (Receiver != "" && Content != "")
                {
                    CookieContainer RequestCookie = GetCookie(UserName, PassWord);
                    if (RequestCookie != null && Token != "")
                    {
                        CookieContainer cookie = Login(UserName, PassWord, Token, RequestCookie);
                        if (cookie != null)
                        {
                            SendArticle(Token, cookie, Receiver, Content);
                        }
                    }
                }
                else
                {
                    Response.Write("<script>alert('艾特用户和推文内容不能为空')</script>");
                }
                return View("Index");
            }
    
            public void SendArticle(string Token, CookieContainer cookie, string Receiver, string Content)//艾特用户
            {
                string PostStr = "authenticity_token=" + Token + "&is_permalink_page=false&page_context=profile&place_id=&status=@" + Receiver + "+" + HttpUtility.UrlEncode(Content, Encoding.UTF8) + "&tagged_users=";
                HttpHelper http = new HttpHelper();
                string html = http.GetHtml("https://twitter.com/i/tweet/create", cookie, PostStr, true);
                if (html.Contains("推文已发送"))
                {
                    Response.Write("<script>alert('您给@" + Receiver + "的推文已发送')</script>");
                }
            }
    
            public ActionResult Reply(string Url, string Content)
            {
                if (Url != "" && Content != "")
                {
                    CookieContainer RequestCookie = GetCookie(UserName, PassWord);
                    if (RequestCookie != null && Token != "")
                    {
                        CookieContainer cookie = Login(UserName, PassWord, Token, RequestCookie);
                        if (cookie != null)
                        {
                            int index1 = Url.LastIndexOf("/");
                            string ReplyId = Url.Substring(index1 + 1);
                            int index2 = Url.LastIndexOf("/", index1 - 1);
                            int index3 = Url.LastIndexOf("/", index2 - 1);
                            string Receiver = Url.Substring(index3 + 1, index2 - index3 - 1);
                            SendReply(Token, cookie, ReplyId, Receiver, Content);
                        }
                    }
                }
                else
                {
                    Response.Write("<script>alert('回复网址和回复内容不能为空')</script>");
                }
                return View("Index");
            }
    
            public void SendReply(string Token, CookieContainer cookie, string ReplyId, string Receiver, string Content)//回复评论
            {
                string PostStr = "authenticity_token=" + Token + "&in_reply_to_status_id=" + ReplyId + "&is_permalink_page=true&place_id=&status=%40" + Receiver + "+" + HttpUtility.UrlEncode(Content, Encoding.UTF8) + "&tagged_users=";
                HttpHelper http = new HttpHelper();
                string html = http.GetHtml("https://twitter.com/i/tweet/create", cookie, PostStr, true);
                if (html.Contains(@"u003e"))
                {
                    Response.Write("<script>alert('你给@" + Receiver + "的评论回复成功')</script>");
                }
            }

    附加HttpHelper封装类下载地址:http://pan.baidu.com/s/1hsls2Ba

    拖进程序直接用

  • 相关阅读:
    顺序栈用C语言实现
    对队列的操作和算法
    对链表的操作与算法
    对动态数组的操作与算法
    链表
    冒泡排序
    指针之动态分配内存
    字符串匹配KMP算法
    DS二叉树--层次遍历
    DS图--最小生成树
  • 原文地址:https://www.cnblogs.com/a849788087/p/5803315.html
Copyright © 2020-2023  润新知