C# 模拟登入微博,实现分享

最近自己想模拟微博登入(无验证码的情况),想分享一些歌曲。很多代码都是网上找到,现在整理一下。

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().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, , 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,后面分享需要用到<br />

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[].Split('=')[] +
                          "&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, , 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

var

相关推荐