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请求 ...
随机推荐
- Lex与Yacc学习(九)之Yacc语法
Yacc语法 本文讨论yacc语法的格式并描述可用的各种特征和选项 yacc语法结构 yacc语法包括三部分:定义段.规则段和用户子例程段 ...定义段... %% ...规则段... %% ...用 ...
- strcpy与strcat函数原型
1.strcpy函数原型 char *my_strcpy(char *dest,const char *src) //const使在函数中不能修改*src其原先的值{ char *strDest ...
- Python3的基本数据类型及常用的方法
python3的基本数据类型: 在python3当中有这么几种基本的数据类型:int(整形).str(字符串).list(列表).tuple(元组).dict(字典).bool(布尔值)等.数字整体划 ...
- 安装mongodb卡顿
"3.6下载安装会卡死."的原因在于,默认安装是包含mongodb compass,这个包的,这个包大约有180MB,下载很慢,导致所谓的的”卡死“. 如果你选用自定义安装的并且不 ...
- 数据库---大数据+hadoop
大数据:hadoop:大数据和hadoop的关系
- 【04】在 PR 中关闭 issue
[04]在 PR 中关闭 issue 似乎要给别人PR. 比如你在创建一个 pull request 去修复 issue #234.那你可在 PR 输入「fixes #234」,就可以自动 ...
- [LoadRunner]LR性能测试结果样例分析
R性能测试结果样例分析 测试结果分析 LoadRunner性能测试结果分析是个复杂的过程,通常可以从结果摘要.并发数.平均事务响应时间.每秒点击数.业务成功率.系统资源.网页细分图.Web服务器资源. ...
- 【LeetCode】Count and Say(报数)
这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...
- json的两种表示结构(对象和数组).。
JSON有两种表示结构,对象和数组.对象结构以”{”大括号开始,以”}”大括号结束.中间部分由0或多个以”,”分隔的”key(关键字)/value(值)”对构成,关键字和值之间以”:”分隔,语法结构如 ...
- 九度oj 题目1172:哈夫曼树
题目描述: 哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和. 输入: 输入有 ...