由于目前电脑网页版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 实现发送消息、评论帖子的更多相关文章

  1. C#实现模拟登录百度并发送私信

    首先获取Token,根据Token获取PubliKey,使用RSA加密POST数据 private Regex _regex = new Regex(@"\{.*\}", Rege ...

  2. Linux给指定用户或全部用户(已登录)发送消息

    在局域网络内很多时候是许多人共用一些机器,但如果多个人同时在使用同一台机器必定会发生一些冲突,比如系统的某些配置被修改,这样引起一些麻烦.那么如果在使用该机器之前,先给登录到该机器的所有其他用户发送一 ...

  3. Scrapy用Cookie实现模拟登录

    模拟登录是爬取某些站点内容的一个关键,有些网站(特别是论坛类),不登录的话,一个数据也拿不到. 模拟登录有这样几个关键: 弄清楚登录的url一些网站打开出现登录的页面,地址栏大多数不是登录提交表单的u ...

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

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

  5. C#模拟百度登录并到指定网站评论回帖(一)

    核心信息: 请求网址:  https://passport.baidu.com/v2/api/?login请求方法:  POST状态码:  HTTP/1.1 200 OK请求头  //用户代理 Use ...

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

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

  7. libcurl模拟登录CSDN并自动评论资源以获取积分

    软件及源码下载:(http://pan.baidu.com/s/1jGE52pK) 涉及到的知识点: C++多线程编程 libcurl的使用(包括发送http请求.发送cookie给服务器.保存coo ...

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

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

  9. 开源微信Http协议Sdk【实现登录/获取好友列表/修改备注/发送消息】

    基于微信Http协议封装的一个Sdk,目前实现了以下功能:. 1:扫码登录(检测二维码扫描状态) 2:获取最近联系人.群组.所有联系人 3:修改好友备注 4:给好友发送消息 暂且这么多,也没多余的时间 ...

随机推荐

  1. Java使用Robot完成QQ轰炸机

    效果 网上吵架吵不过别人怎么办?女朋友让你从1数到10000怎么办?想恶搞朋友怎么办?QQ轰炸机你值得拥有!(注:为了更好的学习编程,敲的练手项目,仅作学习使用) 自定义发送内容,自定义发送条数,&q ...

  2. Eclipse如何创建模拟器

    Eclipse如何创建模拟器下载地址:http://developer.android.com/sdk/index.html#downloadJDK安装包: 1, 打开安卓模拟器控制台(windows ...

  3. Installing pip on CentOS 7 for Python

    nstalling pip on CentOS 7 for Python 2.x On CentOS 7, you have to install setup tools first, and the ...

  4. LightOJ——1012Guilty Prince(连通块并查集)

    1012 - Guilty Prince Time Limit: 2 second(s) Memory Limit: 32 MB Once there was a king named Akbar. ...

  5. 西南民大oj 1762 我的式子不可能那么难写 【波兰式】

    描述 啦啦啦.作为一个苦逼的程序猿.?.请看下图... 现在老总想让你帮他儿子写个简单计算器(他儿子小学3年级,嘘!),写不出来就扣奖金..快帮他写吧... 给一个包含+-*/()的正确的表达式.要你 ...

  6. Error处理: “非法字符: \65279”的解决办法

    将eclipse项目转为maven项目的时候,编译时遇到 “非法字符: \65279”的报错. 出错内容是: *.java:1: 非法字符: \65279    [javac] package com ...

  7. 转 C++STL之string

    http://www.cnblogs.com/wangkangluo1/archive/2011/07/22/2114118.html string类的构造函数: string(const char ...

  8. Codeforces Round #265 (Div. 2) C 暴力+ 找规律+ 贪心

    C. No to Palindromes! time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. CodeForces 379D 暴力 枚举

    D. New Year Letter time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  10. 标准C程序设计七---23

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...