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请求 ...
随机推荐
- spring-cloud-sleuth 学习资源
https://www.baeldung.com/spring-cloud-sleuth-single-application https://howtodoinjava.com/spring-clo ...
- luogu3159 [CQOI2012]交换棋子
把每个点拆成 x y z 对于第 i 个点,x->y是表示流入的,y->z是表示流出的. #include <iostream> #include <cstring> ...
- wordpress需要FTP用户名密码的问题
wordpress安装删除插件需要FTP用户名密码的问题 方法一: 服务器命令操作: 1.在wordpress目录下面wp-config.php末尾加入下面代码: if(is_admin()) { ...
- get 发送ajax请求
上demo小案例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- Python2.6.6升级2.7.3
Python2.7替换2.6: 1.下载Python-2.7.3 #wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 2.解压 ...
- BZOJ-1269 文本编辑器
.... 这道题就是Noi原题嘛...虽然更容易写... 题意: 建立一个数据结构,并支持以下操作: Insert 区间插入有序序列:Delete 区间删除:Rotate 区间翻转:Get 单点查询 ...
- Spoj-ANTP Mr. Ant & His Problem
Mr. Ant has 3 boxes and the infinite number of marbles. Now he wants to know the number of ways he c ...
- mock数据。根据表中一天的数据模拟其他日期的数据
package test; import java.sql.*; import java.text.SimpleDateFormat; import java.util.*; import java. ...
- FGrowth算法
一:背景 http://www.cnblogs.com/aijianiula/p/5397857.html 上节中,总结了频繁项集挖掘的最基本算法:Apriori算法.这篇文章写下它的改进算法FGro ...
- git fetch tag 获取远程tag
获取远程的tag( 远程存在,本地不存在) git fetch origin tag 2.4.7 出现如下文字,说明获取远程tag成功 remote: Counting objects: 2, don ...