这次做成了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 发送私信、艾特用户、回复评论的更多相关文章

  1. 模拟登录,发送amf类型数据

    参考 http://blog.csdn.net/amandag/article/details/5666219 以及 稍微修改了一下AMFPost的类     一.登录 登录过程中主要用到标红的3个请 ...

  2. 使用JAVA实现模拟登陆并发送新浪微博(非调用新浪API)

    没有调用新浪的API,在程序中加入自己的帐号和密码就能发送微博,代码完全在后台运行,不用打开浏览器. 用了HtmlUnit这个库来模拟登录还有发送微博. 先上效果图: 这个是刚登陆上获取第一页的信息. ...

  3. .Net HttpClient 模拟登录微信公众平台发送消息

    1.模拟登录 public WeiXinRetInfo ExecLogin(string name, string pass) { CookieContainer cc = new CookieCon ...

  4. C#模拟登录Facebook 实现发送消息、评论帖子

    由于目前电脑网页版FB实现模拟登录比较困难,本次选择了FB的手机版页面进行登录 MVC: private static string UserName = "用户名"; priva ...

  5. Cookies与保持登录(新浪微博的简单模拟登录)

    Cookies与保持登录(新浪微博的简单登录) .note-content {font-family: "Helvetica Neue",Arial,"Hiragino ...

  6. Python requests模拟登录

    Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...

  7. 使用ImitateLogin模拟登录百度

    在之前的文章中,我已经介绍过一个社交网站模拟登录的类库:imitate-login ,这是一个通过c#的HttpWebRequest来模拟网站登录的库,之前实现了微博网页版和微博Wap版:现在,模拟百 ...

  8. PHP cURL应用实现模拟登录与采集使用方法详解

    对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情景,file_get_co ...

  9. VC使用libcurl模拟登录CSDN并自动评论资源以获取积分

    环境:Win7 64位+VC2008 软件及源码下载:(http://pan.baidu.com/s/1jGE52pK) 涉及到的知识点: C++多线程编程 libcurl的使用(包括发送http请求 ...

随机推荐

  1. sublime__最全面的 Sublime Text 使用指南

    感谢大佬--> 原文链接 摘要(Abstract) 本文系统全面的介绍了Sublime Text,旨在成为最优秀的Sublime Text中文教程. 前言(Prologue) Sublime T ...

  2. Impala的分布式查询

    翻译自<Getting Started with Impala> 分布式查询 分布式查询是impala的核心.曾几何时,你需要研究并行计算,才能开始进行深奥而晦涩的操作.现在,有运行在Ha ...

  3. 1. Go的安装和第一行代码

    Go 语言环境安装 Go 语言支持以下系统: Linux FreeBSD Mac OS X(也称为 Darwin) Windows 安装包下载地址为:https://golang.org/dl/. 如 ...

  4. STM32F407 窗口看门狗 个人笔记

    窗口看门狗的喂狗时间范围 由框图知: 复位条件是:当且仅当 { 启动位启动 且 { T6为0 (计数器的值减小到0X03F即下限,还没喂狗,即喂狗太晚) 或 计数器的值高于上限时喂狗,即喂狗太早 } ...

  5. POJ 1741 Tree ——点分治

    [题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...

  6. BZOJ 1197: [HNOI2006]花仙子的魔法【DP】

    Description 相传,在天地初成的远古时代,世界上只有一种叫做“元”的花.接下来,出 现了一位拥有魔法的花仙子,她能给花附加属性,从此,“元”便不断变异,产生了大千世界千奇百怪的各种各样的花. ...

  7. Linux 下运行 C++ 程序出现 “段错误(核心已转储)”

    Linux下写C++程序出现“段错误(核心已转储)”的问题: 段错误一般就是指访问的内存超出了系统所给这个程序的内存空间,通常这个值是由gdtr来保存的,他是一个48位的寄存器,其中的32位是保存由它 ...

  8. 货车运输(codevs 3287)

    题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过 ...

  9. POJ 3099 Go Go Gorelians

    http://poj.org/problem?id=3099 树的重心:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心 求树的重心 如何在点中构造符合条件的树 得到树后 从 ...

  10. Codeforces Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]

    E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...