C#模拟登录Facebook 实现发送消息、评论帖子
由于目前电脑网页版FB实现模拟登录比较困难,本次选择了FB的手机版页面进行登录
MVC:
private static string UserName = "用户名";
private static string PassWord = "密码"; public ActionResult Index()
{
return View();
} public CookieContainer Login()
{
CookieContainer cookie = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://m.facebook.com");
//WebProxy Proxy = new WebProxy("127.0.0.1", 1080);
//request.Proxy = Proxy;
request.Method = "GET";
request.CookieContainer = cookie;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
request.Accept = "*/*";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream ResponseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(ResponseStream, Encoding.UTF8);
string html = sr.ReadToEnd();
sr.Close();
ResponseStream.Close(); string li = Regex.Match(html, @"<input type=""hidden"" name=""li"" value=""(.*?)"" />").Groups[].Value;
string lsd = Regex.Match(html, @"<input type=""hidden"" name=""lsd"" value=""(.*?)"" autocomplete=""off"" />").Groups[].Value;
string m_ts = Regex.Match(html, @"<input type=""hidden"" name=""m_ts"" value=""(.*?)"" />").Groups[].Value;
string PostStr = "lsd=" + lsd + "&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&version=1&ajax=0&width=0&pxr=0&gps=0&dimensions=0&m_ts=" + m_ts + "&li=" + li + "&email=" + UserName + "&pass=" + PassWord + "&login=%E7%99%BB%E5%BD%95"; return Post(PostStr, cookie);
} public CookieContainer Post(string PostStr, CookieContainer cookie)
{
byte[] PostData = Encoding.UTF8.GetBytes(PostStr);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://m.facebook.com/login.php?refsrc=https://m.facebook.com/&lwv=100&refid=8");
//WebProxy Proxy = new WebProxy("127.0.0.1", 1080);
//request.Proxy = Proxy;
request.Method = "POST";
request.CookieContainer = cookie;
request.Referer = "https://m.facebook.com/";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.KeepAlive = true;
request.ContentLength = PostData.Length;
Stream RequestStream = request.GetRequestStream();
RequestStream.Write(PostData, , PostData.Length);
RequestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream ResponseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(ResponseStream, Encoding.UTF8);
string html = sr.ReadToEnd();
sr.Close();
ResponseStream.Close();
return cookie;
} public ActionResult Msg(string UserId, string Content)
{
SendMsg(Login(), UserId, Content); return View("Index");
} public void SendMsg(CookieContainer cookie, string UserId, string Content)//发送消息
{
HttpHelper http = new HttpHelper();
string MsgUrl = "https://m.facebook.com/messages/thread/" + UserId;
string html = http.GetHtml(MsgUrl, cookie, MsgUrl);
string fb_dtsg = Regex.Match(html, @"<input type=""hidden"" name=""fb_dtsg"" value=""(.*?)"" autocomplete=""off"" />").Groups[].Value;
string PostStr = "fb_dtsg=" + fb_dtsg + "&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&ids%5B0%5D=" + UserId + "&photo=&body=" + HttpUtility.UrlEncode(Content, Encoding.UTF8) + "&Send=%E5%8F%91%E9%80%81&waterfall_source=message"; string CallBack = http.GetHtml("https://m.facebook.com/messages/send/?icm=1", cookie, PostStr, true);
if (CallBack.Contains("找不到内容") || CallBack.Contains("你请求的页面无法显示"))
{
Response.Write("<script>alert('发送失败')</script>");
}
else
{
Response.Write("<script>alert('发送成功')</script>");
}
} public ActionResult Reply(string Url, string Content)
{
SendReply(Login(), Url, Content); return View("Index");
} public void SendReply(CookieContainer cookie, string Url, string Content)//发送评论
{
string fbid = GetString(FindString(Url, "fbid"));
string id = GetString(FindString(FindString(Url, "fbid"), "id")); HttpHelper http = new HttpHelper();
string html = http.GetHtml(Url, cookie, Url);
string fb_dtsg = Regex.Match(html, @"<input type=""hidden"" name=""fb_dtsg"" value=""(.*?)"" autocomplete=""off"" />").Groups[].Value;
string PostUrl = "https://m.facebook.com/a/comment.php?fs=8&fr=%2Fprofile.php&actionsource=13&comment_logging&ft_ent_identifier=" + fbid + "&gfid=AQCRDzM_Y5K_3Xk9&_ft_=top_level_post_id." + fbid + "%3Atl_objid." + fbid + "%3Athid." + id + "&av=100013151650782&refid=52";
string PostStr = "fb_dtsg=" + fb_dtsg + "&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&comment_text=" + HttpUtility.UrlEncode(Content, Encoding.UTF8); string CallBack = http.GetHtml(PostUrl, cookie, PostStr, true);
if (CallBack.Contains("找不到内容") || CallBack.Contains("你请求的页面无法显示"))
{
Response.Write("<script>alert('评论失败,该用户已设置权限')</script>");
}
else
{
Response.Write("<script>alert('评论成功')</script>");
}
} public string FindString(string Content, string Str)
{
int index = Content.IndexOf(Str);
if (index >= )
{
return Content.Substring(index + Str.Length, Content.Length - index - Str.Length);
}
return "";
} public string GetString(string Content)
{
int index = Content.IndexOf("&");
if (index >= )
{
return Content.Substring(, index - );
}
return "";
}
C#模拟登录Facebook 实现发送消息、评论帖子的更多相关文章
- C#实现模拟登录百度并发送私信
首先获取Token,根据Token获取PubliKey,使用RSA加密POST数据 private Regex _regex = new Regex(@"\{.*\}", Rege ...
- Linux给指定用户或全部用户(已登录)发送消息
在局域网络内很多时候是许多人共用一些机器,但如果多个人同时在使用同一台机器必定会发生一些冲突,比如系统的某些配置被修改,这样引起一些麻烦.那么如果在使用该机器之前,先给登录到该机器的所有其他用户发送一 ...
- Scrapy用Cookie实现模拟登录
模拟登录是爬取某些站点内容的一个关键,有些网站(特别是论坛类),不登录的话,一个数据也拿不到. 模拟登录有这样几个关键: 弄清楚登录的url一些网站打开出现登录的页面,地址栏大多数不是登录提交表单的u ...
- .Net HttpClient 模拟登录微信公众平台发送消息
1.模拟登录 public WeiXinRetInfo ExecLogin(string name, string pass) { CookieContainer cc = new CookieCon ...
- C#模拟百度登录并到指定网站评论回帖(一)
核心信息: 请求网址: https://passport.baidu.com/v2/api/?login请求方法: POST状态码: HTTP/1.1 200 OK请求头 //用户代理 Use ...
- VC使用libcurl模拟登录CSDN并自动评论资源以获取积分
环境:Win7 64位+VC2008 软件及源码下载:(http://pan.baidu.com/s/1jGE52pK) 涉及到的知识点: C++多线程编程 libcurl的使用(包括发送http请求 ...
- libcurl模拟登录CSDN并自动评论资源以获取积分
软件及源码下载:(http://pan.baidu.com/s/1jGE52pK) 涉及到的知识点: C++多线程编程 libcurl的使用(包括发送http请求.发送cookie给服务器.保存coo ...
- 模拟登录,发送amf类型数据
参考 http://blog.csdn.net/amandag/article/details/5666219 以及 稍微修改了一下AMFPost的类 一.登录 登录过程中主要用到标红的3个请 ...
- 开源微信Http协议Sdk【实现登录/获取好友列表/修改备注/发送消息】
基于微信Http协议封装的一个Sdk,目前实现了以下功能:. 1:扫码登录(检测二维码扫描状态) 2:获取最近联系人.群组.所有联系人 3:修改好友备注 4:给好友发送消息 暂且这么多,也没多余的时间 ...
随机推荐
- JavaScript正则表达式-反向引用
使用括号“()”进行分组,使子表达式(子模式)可以作为整体独立被修饰,子表达式所匹配的结果会被记录下来并可以单独被访问. /(a(b(cd){2})+)EF/ 则各引用分别对应: \1 对应(a(b ...
- PAT Basic 1040
1040 有几个PAT 字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串 ...
- slave_net_timeout 问题一则
[背景] 对一套数据库集群进行5.5升级到5.6之后,alter.log 报warning异常. 2015-02-03 15:44:51 19633 [Warning] Storing MySQL ...
- python中判断字符串是否为中文
判断字符串是否在中文编码范围内 for c in s: if not ('\u4e00' <= c <= '\u9fa5'): return False ...
- 九度oj 题目1020:最小长方形
题目描述: 给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在内.长方形框的边分别平行于x和y坐标轴,点落在边上也算是被框在内. 输入: 测试输入 ...
- 如何部署 sources and javadoc jars
mvn org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy-file -Durl=file:///home/me/m2-repo \ - ...
- DDLog-不同颜色打印信息
(一)下载安装 1.安装插件 XcodeColors Github 链接:https://github.com/robbiehanson/XcodeColors 打开XcodeColors项目,编译即 ...
- BZOJ 3143 [Hnoi2013]游走 ——概率DP
概率DP+高斯消元 与博物馆一题不同的是,最终的状态是有一定的概率到达的,但是由于不能从最终状态中出来,所以最后要把最终状态的概率置为0. 一条边$(x,y)$经过的概率是x点的概率$*x$到$y$的 ...
- BZOJ1925 [Sdoi2010]地精部落 【dp】
题目 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi,其中Hi是1到N ...
- BZOJ3996 [TJOI2015]线性代数 【最小割】
题目 给出一个NN的矩阵B和一个1N的矩阵C.求出一个1*N的01矩阵A.使得 D=(AB-C)A^T最大.其中A^T为A的转置.输出D 输入格式 第一行输入一个整数N,接下来N行输入B矩阵,第i行第 ...