由于目前电脑网页版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. Aizu-ALDS1_3_A:Stack

    D - Stack Write a program which reads an expression in the Reverse Polish notation and prints the co ...

  2. python中set()函数的用法

    set顾名思义是集合,里面不能包含重复的元素,接收一个list作为参数 list1=[1,2,3,4] s=set(list1) print(s) #逐个遍历 for i in s: print(i) ...

  3. Python中sys.argv的用法

    sys.argv是获取运行python文件的时候命令行参数 下面的代码文件是a.py,当我不用IDE工具,只用命令行窗口运行的时候,进入文件所在目录,输入:python a.py 输出结果如下 imp ...

  4. Django中的csrf相关装饰器

    切记:  这俩个装饰器不能直接加在类中函数的上方 (CBV方式) csrf_exempt除了,csrf_protect受保护的   from django.views import Viewfrom ...

  5. C#简易日志输出

    精简版: public static void WriteLog(string message, string group = "") { var logPath = System ...

  6. Eclipse截取android报错log

    Eclipse截取android报错log: 1.前提条件:已安装eclipse 2. LogCat界面设置: Logcat是Android 编程中一个命令行工具,可以用于得到程序的 log 信息,可 ...

  7. 如何使用百度地图API

    一.申请密钥 1.先用eclipse创建一个Android工程 2.在百度api官网上申请一个密钥,链接:http://lbsyun.baidu.com/apiconsole/key 二.工程配置 1 ...

  8. 【JavaScript 12—应用总结】:弹出登录框

    导读:上篇博客中,做好了个人中心的下拉菜单,这次,将做每个网站都会有的一个登录功能,以此类推,可以做出别的想要的弹出框,如错误提示啦,或者注册. 一.实现分析 首先:和下拉菜单一样,需要通过CSS样式 ...

  9. VM上完美运行macos

    VM上完美运行macos 作者:方辰昱 时间:十月三号 效果图 简要步骤 下载安装VM 下载镜像文件链接,darwin.iso,unlocker,beamoff.合集下载链接:https://pan. ...

  10. BZOJ 2780 [Spoj]8093 Sevenk Love Oimaster ——广义后缀自动机

    给定n个串m个询问,问每个串在n个串多少个串中出现了. 构建广义后缀自动机,(就是把所有字符串的后缀自动机合并起来)其实只需要add的时候注意一下就可以了. 然后对于每一个串,跑一边匹配,到达了now ...