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 = ;
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();
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[].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 = ;
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, , 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(""))
{
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 + );
int index2 = Url.LastIndexOf("/", index1 - );
int index3 = Url.LastIndexOf("/", index2 - );
string Receiver = Url.Substring(index3 + , index2 - index3 - );
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#模拟登录Twitter 发送私信、艾特用户、回复评论的更多相关文章
- 模拟登录,发送amf类型数据
参考 http://blog.csdn.net/amandag/article/details/5666219 以及 稍微修改了一下AMFPost的类 一.登录 登录过程中主要用到标红的3个请 ...
- 使用JAVA实现模拟登陆并发送新浪微博(非调用新浪API)
没有调用新浪的API,在程序中加入自己的帐号和密码就能发送微博,代码完全在后台运行,不用打开浏览器. 用了HtmlUnit这个库来模拟登录还有发送微博. 先上效果图: 这个是刚登陆上获取第一页的信息. ...
- .Net HttpClient 模拟登录微信公众平台发送消息
1.模拟登录 public WeiXinRetInfo ExecLogin(string name, string pass) { CookieContainer cc = new CookieCon ...
- C#模拟登录Facebook 实现发送消息、评论帖子
由于目前电脑网页版FB实现模拟登录比较困难,本次选择了FB的手机版页面进行登录 MVC: private static string UserName = "用户名"; priva ...
- Cookies与保持登录(新浪微博的简单模拟登录)
Cookies与保持登录(新浪微博的简单登录) .note-content {font-family: "Helvetica Neue",Arial,"Hiragino ...
- Python requests模拟登录
Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...
- 使用ImitateLogin模拟登录百度
在之前的文章中,我已经介绍过一个社交网站模拟登录的类库:imitate-login ,这是一个通过c#的HttpWebRequest来模拟网站登录的库,之前实现了微博网页版和微博Wap版:现在,模拟百 ...
- PHP cURL应用实现模拟登录与采集使用方法详解
对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情景,file_get_co ...
- VC使用libcurl模拟登录CSDN并自动评论资源以获取积分
环境:Win7 64位+VC2008 软件及源码下载:(http://pan.baidu.com/s/1jGE52pK) 涉及到的知识点: C++多线程编程 libcurl的使用(包括发送http请求 ...
随机推荐
- Oracle中Restore和Recovery的区别
一.参考解释一 在Oracle的备份与恢复的知识点中,经常会出现Restore 和 Recovery两个词. 由于这两个词在字典中的解释很接近,困扰了我很久.直到我在Oracle的官方文档中看到了以下 ...
- json分享
JSON是什么? JavaScript Object Notation (JSON) is a text format for the serialization of structured data ...
- A Survey of Model Compression and Acceleration for Deep Neural Network时s
A Survey of Model Compression and Acceleration for Deep Neural Network时s 本文全面概述了深度神经网络的压缩方法,主要可分为参数修 ...
- Codeforces Round #416 (Div. 2) 本来以为这个时间是晚上的,下午就没做
A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 九度oj 题目1088:剩下的树
题目描述: 有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L ...
- Selenium WebDriver高级用法
Selenium GitHub地址 选择合适的WebDrvier WebDriver是一个接口,它有几种实现,分别是HtmlUnitDrvier.FirefoxDriver.InternetExplo ...
- nginx的正则
~ 为区分大小写的匹配. ~* 不区分大小写的匹配(匹配firefox的正则同时匹配FireFox). !~ 不匹配的 !~* 不匹配的 . 匹配除换行符以外的 ...
- SPOJ GSS6 Can you answer these queries VI ——Splay
[题目分析] 增加了插入和删除. 直接用Splay维护就好辣! 写了一个晚上,(码力不精),最后发现更新写挂了 [代码] #include <cstdio> #include <cs ...
- 【CCF】棋局评估
博弈论极小极大搜索,记忆化+状压 #include<iostream> #include<cstdio> #include<string> #include< ...
- cf21D Traveling Graph
You are given undirected weighted graph. Find the length of the shortest cycle which starts from the ...