这次做成了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. ctci(1)

    // // main.cpp // proj1 // // Created by Yuxin Kang on 8/24/14. // Copyright (c) 2014 Yuxin Kang. Al ...

  2. CodeForces 149D 区间DP Coloring Brackets

    染色有三个条件: 对于每个点来说要么不染色,要么染红色,要么染蓝色 对于每对配对的括号来说,有且只有一个一边的括号被染色 相邻的括号不能染成相同的颜色 首先可以根据给出的括号序列计算出括号的配对情况, ...

  3. Ubuntu 清理卸载残留文件

    dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P

  4. python之动态参数 *args,**kwargs(聚合,打散)

    一.函数的动态参数 *args,**kwargs, 形参的顺序1.你的函数,为了拓展,对于传入的实参数量应该是不固定,所以就需要用到万能参数,动态参数,*args, **kwargs 1,*args  ...

  5. kali2018利用ss和ProxyChains实现任意应用代理

    第一步:配置ss 第二步:配置proxychain vim /etc/proxychains.conf 第三步:使用proxychains 终端输入: proxychains firefox 通过代理 ...

  6. Leetcode 336.回文对

    回文对 给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串. 示例 1: 输入: ["abcd&quo ...

  7. Spoj-FACVSPOW Factorial vs Power

    Consider two integer sequences f(n) = n! and g(n) = an, where n is a positive integer. For any integ ...

  8. Java 学习(3):java 对象和类

    目录: --- 对象 --- 类 --- 源文件的声明规则 --- Java 包 对象: 对象是类的一个实例(对象不是找个女朋友),有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种: ...

  9. 【HDOJ5952】Counting Cliques(团,dfs)

    题意:给定一张n点m边的图,求大小为S的团的个数 N ≤ 100,M ≤ 1000,2 ≤ S ≤ 10,保证点的度不超过20 思路:dfs 因为每个点可能不止属于一个极大团,所以不能求出极大团然后计 ...

  10. Android SurfaceView与View

    SurfaceView介绍 SurfaceView是视图(View)的继承类,这个视图里面内嵌了一个专门用于绘制的Surface.你可以控制这个Surface的格式和尺寸,而SurfaceView控制 ...