zoukankan      html  css  js  c++  java
  • 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[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 "";
            }
  • 相关阅读:
    zabbix
    Tomcat 部署Web应用
    WebLogic
    WebLogic部署集群和代理服务器
    WebLogic 中的基本概念
    GIT详解和GITHUB
    Tomcat 内存溢出对应解决方式
    apache和tomcat区别
    数据库介绍
    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
  • 原文地址:https://www.cnblogs.com/a849788087/p/5846312.html
Copyright © 2011-2022 走看看