static void Main(string[] args) { GetCookie("UserName", "PassWord"); Console.ReadLine(); } public static void GetCookie(string UserName, string PassWord) { HttpWebRequest request = null; HttpWebResponse response = null; try { CookieContainer cookie = new CookieContainer(); request = (HttpWebRequest)HttpWebRequest.Create("https://twitter.com/login/"); 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(); //string Token = Regex.Match(content, @"<input type=""hidden"" value=""(\w+)"" name=""authenticity_token""/>").Groups[1].Value; //获取Token字符串 HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(content); string XPath = "//input[@name=‘authenticity_token‘]"; if (doc.DocumentNode.SelectNodes(XPath) != null) { string Token = doc.DocumentNode.SelectSingleNode(XPath).Attributes["value"].Value; Login(UserName, PassWord, Token, cookie); } } catch (Exception) { request.Abort(); response.Close(); Console.WriteLine("获取Token字符串失败,请检查网络连接"); } } public static void 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"); 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("查看个人资料")) { Console.WriteLine("Twitter登录成功"); SendMsg(Token, cookie); } else { Console.WriteLine("Twitter登录失败"); } } catch (Exception) { request.Abort(); response.Close(); Console.WriteLine("登录失败,请检查网络连接"); } } public static void SendMsg(string Token, CookieContainer cookie) { Console.WriteLine("-----------发送私信-----------"); Console.Write("请输入收件人:"); string Receiver = Console.ReadLine(); Console.Write("请输入发送内容:"); string Msg = Console.ReadLine(); string PostStr = "authenticity_token=" + Token + "&screen_name%5B%5D=" + Receiver + "&scribeContext%5Bcomponent%5D=tweet_box_dm&tagged_users=&text=" + Msg + "&tweetboxId=swift_tweetbox_1472018056979"; HttpHelper http = new HttpHelper(); string html = http.GetHtml("https://twitter.com/i/direct_messages/new", cookie, PostStr, true); Console.WriteLine(html); if (html.Contains("该用户没有关注你") || html.Contains("你只可以发送私信给关注你的人")) { Console.WriteLine("您输入的用户名不存在,或该用户没有关注你,你只可以发送私信给关注你的人"); } else if (html.Contains(@"\u003e")) { Console.WriteLine("发送成功"); } }
HttpHelper为HttpWebRequest封装类。
时间: 2024-10-04 20:42:27