c#推特登录发推评论
第一次写博客
用c#做了一个有关推特的模拟登录,评论等内容。
开始用c#写时,对c#也就是写过学校的数据库实验课设。然后搜到了有人写过关于c#模拟登录twitter的,就去学习了下,用一段时间弄懂用c#去登录的基本思路,然后自己接着才又做了发推,评论,盖楼,改头像,改密码。非常感谢那位 博主 写的模拟登陆的代码给了我学习的地方!
首先推特登录没有验证码,这也是省去很多麻烦,通过抓包尝试看看登录所需信息
URL:https://twiitter.com/sessions
用户名密码我随便填的,除此之外,还有个token,ui_metrics两个数据需要提交的。这些可以用c#做爬虫实时爬下来然后放入登录请求中,思路也不是很难,就当初对C#爬虫这块不太了解,又去学习了一小会,反正它也没验证码,经过测试最后也是可以成功模拟登录成功了。
public CookieContainer GetCookie( string UserAgent,ref string Token)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
CookieContainer cookie = null;
try
{
cookie = new CookieContainer();
request = (HttpWebRequest)HttpWebRequest.Create("https://twitter.com/login/")
request.Timeout = 18000;
request.Method = "GET";
request.Host = "twitter.com";
request.UserAgent = UserAgent;
request.AddRange(1024);
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[1].Value;
return cookie;
}
catch (Exception)
{
request.Abort();
return cookie;
}
}
下面是模拟登录的函数,在登录前我访问了https://twiitter.com/login 爬取到token,ui_metrics还有cookie。因为有些帐号登录时需要验证邮箱,后来我又加了个自动判断验证邮箱。验证邮箱需要的参数抓包可以知道的,那些参数也用爬虫正则匹配爬下来就好了,chanllengeId ,userid,token这些都是验证邮箱需要的参数。
public CookieContainer Login(string UserName, string PassWord,string Token, CookieContainer cookie, string UserAgent,string ui_metrics,ref string station,ref string chanllengeId,ref string userid)
{
String PostStr = "session%5Busername_or_email%5D=" + UserName + "&session%5Bpassword%5D=" + PassWord + "&return_to_ssl=truee&scribe_log=&redirect_after_login=%2F%3Flogged_out%3D1%26lang%3Den&authenticity_token=" + Token + "&ui_metrics=" + ui_metrics ;
byte[] Data = Encoding.UTF8.GetBytes(PostStr);
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = (HttpWebRequest)WebRequest.Create("https://twitter.com/sessions")
request.Timeout = 18000;
request.Method = "POST";
request.Host = "twitter.com";
request.UserAgent = UserAgent;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*;q=0.8";
request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9";
request.Referer = "https://twitter.com/?logged_out=1&lang=en";
request.CookieContainer = cookie;
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(Data, 0, 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("If you’re not redirected soon"))
{
station = UserName + "登录成功\n";
}
else
{
if (content.Contains("1eqd"))
{
station = "填写邮箱中...\n";
chanllengeId = Regex.Match(content, @"challenge_id=(\w+)&").Groups[1].Value;
userid = Regex.Match(content, @"user_id=(\w+)&").Groups[1].Value;
return null;
}
station = UserName + "登录失败\n";
}
return cookie;
}
catch (Exception)
{
station = UserName + "访问失败\n";
request.Abort();
return cookie;
}
}
这里就返回了登录的cookie,然后可以用它发推,评论,改头像,改名字,改密码,这些通过抓包也不是很难,参照上面代码,抓一下包,post内容换一下差不多都可以了。主要点赞遇到了麻烦,没有做出来,等我有空再研究研究点赞什么情况。
过程中遇到的问题:登录评论时证书之类的出错问题,复制出错提示百度一下,解决了;登录,评论有时post请求与之前不一样,需要重新抓包查看修改post请求内容;带图发推,评论,图片上传的问题,自己也是通过抓包分析,构造请求流,也花了3天左右才成功发有图片内容的推文与评论。主要是图片的请求的顺序,请求内容\n\t一个都不能少,这个卡了我好长时间,这些百度也都有先例,参照了其他人
总结:推特没有微博的验证码,这个是比较好的。最后来看代码不是很难,得多抓包多上网查阅相关资料多尝试,C#语言也多了解了一丢丢,语言只是一种工具,最重要的是对爬虫,模拟登录有了一定的思路。
先写到这吧