1. 程式人生 > >C# 模擬登入微博,實現分享

C# 模擬登入微博,實現分享

sps des item bject dto bstr 分享 ring 求分享

最近自己想模擬微博登入(無驗證碼的情況),想分享一些歌曲。很多代碼都是網上找到,現在整理一下。

1、加載我需要分享頁面的數據。

                var httpReq =
                    (HttpWebRequest)WebRequest.Create("http://pop.weibo.com/ajax_getdata?type=trend&date=" +
                                                       DateTime.Now.ToShortDateString());

                httpReq.Referer 
= "http://pop.weibo.com/"; //指定跳轉之前的地址 Referer Message.Text += Environment.NewLine + @"加載數據開始" + Environment.NewLine; var httpResp = (HttpWebResponse)httpReq.GetResponse(); var respStream = httpResp.GetResponseStream(); if (respStream != null
) { var respStreamReader = new StreamReader(respStream, Encoding.UTF8); var result = respStreamReader.ReadToEnd(); var json1 = (JObject)JsonConvert.DeserializeObject(result); var mulist = new List<Mulist>();
foreach (var item in json1["list"]["data"]) { var mu = new Mulist { songid = item["songid"].ToString(), songname = item["songname"].ToString(), score = int.Parse(item["score"].ToString()) }; mulist.Add(mu); } dataGridView1.DataSource = mulist; Message.Text += Environment.NewLine + @"加載數據成功!" + Environment.NewLine; }

2、分享,分享之前需要進行模擬登入,但是登入之前需要去請求一個地址,獲取登入時需要的參數。

            HttpWebRequest request = null;
            var tim = ConvertDateTimeInt(DateTime.Now);
            var baseun = Convert.ToBase64String(Encoding.Default.GetBytes(username));//請求登入之前賬號為base64格式

   
            var slGhttpurl =
                "https://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.18)&_=" +
                tim + "&su=" + baseun;//登入之前會去請求這個鏈接獲取登入需要參數

            Message.Text += Environment.NewLine + @"請求登入需要參數地址開始:" + slGhttpurl + Environment.NewLine;

            request = (HttpWebRequest)WebRequest.Create(slGhttpurl);
            request.ContentType = "application/x-www-form-urlencoded";

            
            request.KeepAlive = true;
            request.Method = "GET";
            var response = (HttpWebResponse)request.GetResponse();
            var respStream = response.GetResponseStream();
            var respStreamReader = new StreamReader(respStream, Encoding.UTF8);
            var slGresult = respStreamReader.ReadToEnd().Substring(34).Replace(")", "").Replace("(", "");
            request.GetResponse().Close();

            var loginData = JsonConvert.DeserializeObject<LoginData>(slGresult);//密碼加密需要用到的參數

3、請求登入地址,登入關鍵參數還是密碼加密,我在網上找了一段js代碼,結果調用可以成功登入。。js在demo裏面就不放在這裏了

            //調用JS加密密碼   這個需要添加一個dll  Interop.MSScriptControl.dll
            var scriptControl = new ScriptControl
            {
                UseSafeSubset = true,
                Language = "javascript"
            };
            scriptControl.AddCode(GetJs());
            object obj = scriptControl.Run("getpass", password, loginData.servertime, loginData.nonce, loginData.pubkey);
            var pdata = obj.ToString().Trim();

            Message.Text += Environment.NewLine + @"調用js密碼加密成功:" + pdata + Environment.NewLine;

            Message.Text += Environment.NewLine + @"登入開始:"  + Environment.NewLine;
            //開始登入
            var lGhttpurl = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)&_=" +
                            tim;
            request = (HttpWebRequest)WebRequest.Create(lGhttpurl);
            var result1 = "entry=weibo&gateway=1&from=&savestate=0&useticket=1" +
                          "&pagerefer=http://pop.weibo.com/" +
                          "&vsnf=1&su=" + baseun + "&service=miniblog&servertime=" + loginData.servertime +
                          "&nonce=" +
                          loginData.nonce + "&pwencode=rsa2" +
                          "&rsakv=" + loginData.rsakv + "&sp= " + pdata +
                          "&sr=1536*864&encoding=UTF-8&cdult=2&domain=weibo.com&prelt=53&returntype=TEXT";
            var bytes = Encoding.UTF8.GetBytes(result1);
            request.Method = "POST";
            request.KeepAlive = true;
            request.CookieContainer = new CookieContainer();
            request.ContentLength = bytes.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            var reqStream1 = request.GetRequestStream();
            reqStream1.Write(bytes, 0, bytes.Length);
            reqStream1.Close();

            request.GetResponse().Close();
            Message.Text += Environment.NewLine + @"登入成功,cookie:"+ request.CookieContainer.GetCookieHeader(request.RequestUri) + Environment.NewLine;
            return request.CookieContainer.GetCookieHeader(request.RequestUri);//獲取登入cookie,後面分享需要用到

4、獲取分享的地址

        public string Fxurl(string songId)
        {
            HttpWebRequest request2 = null;
            var fXhttpurl = "http://pop.weibo.com/share?songid=" + songId;
            request2 = (HttpWebRequest)WebRequest.Create(fXhttpurl);
            request2.Referer = "http://pop.weibo.com/";
            request2.KeepAlive = true;
            request2.Host = "pop.weibo.com";
            request2.CookieContainer = new CookieContainer();
            var response2 = (HttpWebResponse)request2.GetResponse();
            var sharereferurl = response2.ResponseUri.OriginalString;
            request2.GetResponse().Close();
            return sharereferurl;
        }

5、開始分享,我用抓包的發現會分享的時候回去請求兩個地址,如下。

        /// <summary>
        /// 開始分享
        /// </summary>
        /// <param name="sharereferurl">Referer參數</param>
        /// <param name="sLGcookies">登入的cookie</param>
        /// <returns></returns>
        public string Fx(string sharereferurl, string sLGcookies)
        {
            HttpWebRequest request = null;
            var tim = ConvertDateTimeInt(DateTime.Now);
            var relist = sharereferurl.Split(&);
            //獲取分享內容拼接參數
            var result4 = "content=" + relist[2].Split(=)[1] +
                          "&styleid=1&from=share&appkey=846367063&visible=0";
            var bytes1 = Encoding.UTF8.GetBytes(result4);
            //請求分享
            request = (HttpWebRequest)WebRequest.Create("http://service.weibo.com/share/aj_share.php");
            request.Method = "POST";
            request.Referer = sharereferurl;//指定是哪個地址到的分享地址
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = bytes1.Length;
            request.Headers.Add("Cookie", sLGcookies);//添加之前獲取的登入cookie
            var reqStream3 = request.GetRequestStream();
            reqStream3.Write(bytes1, 0, bytes1.Length);
            reqStream3.Close();
            request.GetResponse().Close();

            request =
                (HttpWebRequest)WebRequest.Create(
                    "http://service.weibo.com/widget/like/aj_like.php?url=&from=thirdparty&app_src=cUcI1A&_t=0&__rnd=" +
                    tim);
            request.Method = "GET";
            request.Headers.Add("Cookie", sLGcookies);
            request.Referer = sharereferurl;//指定是哪個地址到的分享地址
            var response4 = (HttpWebResponse)request.GetResponse();
            var respStream4 = response4.GetResponseStream();
            var respStreamReader4 = new StreamReader(respStream4, Encoding.UTF8);
            var result6 = respStreamReader4.ReadToEnd();
            var json3 = (JObject)JsonConvert.DeserializeObject(result6);//分享結果

            return json3["code"].ToString();
        }

這樣就可以分享成功了,在此記錄一下。

Demo 密碼: cv4q

C# 模擬登入微博,實現分享