由于目前电脑网页版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. 【HIHOCODER 1067】最近公共祖先·二(LCA)

    描述 上上回说到,小Hi和小Ho用非常拙劣--或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个人的所有共同祖先中辈分最低的一个是谁.远在美国的他们利用了一些奇妙的技术获得了国内许多人的 ...

  2. Experimental considerations

    先知 重金属颗粒与气孔大小 气孔位置.密度与开合时间 角质层厚度 意外 叶子第二天掉落 样本没有放冰箱 高光谱仪器损坏 天气下雨/雪 仪器预约 楼顶/实验室门卡提前一天预约 光合仪提前一天预约 ASD ...

  3. 出现Android.os.NetworkOnMainThreadException 错误

    两种方法解决: 1.如果用的gradle打包,在build.gradle中修改配置 修改SDKVersion 为低版本(7),不能版本降低过多,否则会出现很多不适配. 2.将网络访问放在一个新的线程中 ...

  4. luogu2634 聪聪可可

    点分治裸题 #include <iostream> #include <cstdio> using namespace std; int n, uu, vv, ww, ans, ...

  5. 2017 Multi-University Training Contest - Team 2

    Regular polygon Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. 【U+B+D】三层框架 原理+实例

    导读:三层的学习,也终于得到收获了.这个过程很艰辛,不止一次的想放弃.在这一个学习过程中,真的很感谢师傅的尽心.耐心.费心.其实真的很脆弱,现在回想起来都很不可思议. 一.基本概况 1,什么是三层 我 ...

  7. 30分钟学会如何使用Shiro(转自:http://www.cnblogs.com/learnhow/p/5694876.html)

    本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分 ...

  8. [luoguP2495] [SDOI2011]消耗战(DP + 虚树)

    传送门 明显虚树. 别的题解里都是这样说的. 先不考虑虚树,假设只有一组询问,该如何dp? f[u]表示把子树u中所有的有资源的节点都切掉的最优解 如果节点u需要切掉了话,$f[u]=val[u]$ ...

  9. spring之lazy-init

    lazy-init:延迟实例化 ApplicationContext实现的默认行为就是在启动服务器时将所有singleton bean提前进行实例化.提前实例化意味着作为初始化过程的一部分,appli ...

  10. 【DFS序+线段树区间更新区间求最值】HDU 5692 Snacks

    http://acm.hdu.edu.cn/showproblem.php?pid=5692 [思路] 每更新一个点,子树的所有结点都要更新,所以是区间更新 每查询一个点,子树的所有结点都要查询,所以 ...