• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#模拟登录Facebook实现发送消息、评论帖子

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

由于目前电脑网页版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[1].Value;
            string lsd = Regex.Match(html, @"<input type=""hidden"" name=""lsd"" value=""(.*?)"" autocomplete=""off"" />").Groups[1].Value;
            string m_ts = Regex.Match(html, @"<input type=""hidden"" name=""m_ts"" value=""(.*?)"" />").Groups[1].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, 0, 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[1].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[1].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 >= 0)
            {
                return Content.Substring(index + Str.Length, Content.Length - index - Str.Length);
            }
            return "";
        }

        public string GetString(string Content)
        {
            int index = Content.IndexOf("&");
            if (index >= 0)
            {
                return Content.Substring(1, index - 1);
            }
            return "";
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap