九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
c#獲取新浪微博登錄cookie

 用新浪微博api收集數據有諸多限制,每小時只能調用官方api函數150次,認證也很麻煩。因此想通過爬網頁的方式來收集數據。訪問新浪微博用戶網頁首先需要登錄,登錄獲取cookie后可直接獲取網頁數據,無需再次登錄。獲取登錄cookie的方式具體如下:

1)采用get的方式訪問http://login.sina.com.cn/sso/prelogin.php?entry=miniblog&callback=sinaSSOController.preloginCallBack&user=賬戶&client=ssologin.js(v1.3.16),獲取servertime和 nonce,用于之后的密碼加密。注:賬戶值為登錄新浪微博用戶名的base64編碼值。

2)處理新浪微博用戶密碼psw。若hex_sha1(pwd)表示對pwd進行sha1處理,則新浪微博用戶密碼的處理方式表示為hex_sha1(hex_sha1(hex_sha1(psw))+ servertime + nonce)。

3)采用post方式訪問http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.16),并post如下數據:

 string str ="entry=weibo&gateway=1&from=&savestate=7&useticket=1&ssosimplelogin=1&su="+
                登錄賬戶base64值+ "&service=miniblog&servertime=" + servertime +"&nonce=" + nonce + "&pwencode=wsse&sp="+ 密碼處理值 + "&encoding=utf-8&url=" +
               HttpUtility.UrlEncode("

4)如果登錄成功,返回頁面中retcode值為0;get方式訪問網頁中l(wèi)ocation.replace處的地址。

保存這三次訪問頁面的cookie后下次再訪問新浪微博用不再需要重新登錄了。具體代碼如下:

static CookieContainer cc = new CookieContainer();

 

  public static int SinaLogin(string uid,string psw, CookieContainer cc)
       {

           string uidbase64 = Base64Code(uid); //處理登錄賬戶如***@**.com 
           string url = "               + uidbase64 + "&client=ssologin.js(v1.3.16)";
           HttpWebRequest webRequest1 = (HttpWebRequest)WebRequest.Create(newUri(url)); //獲取servertime和 nonce
           webRequest1.CookieContainer = cc;
           HttpWebResponse response1 =(HttpWebResponse)webRequest1.GetResponse();
           StreamReader sr1 = new StreamReader(response1.GetResponseStream(),Encoding.UTF8);
           string res = sr1.ReadToEnd();
           int start = res.IndexOf("servertime");
           if (start < 0 || start >= res.Count()) return -1;
           int end = res.IndexOf(',', start);
           if (end < 0 || end >= res.Count()) return -1;
           string servertime = res.Substring(start + 12, end - start -12);

           start = res.IndexOf("nonce");
           if (start < 0 || start >= res.Count()) return -1;
           end = res.IndexOf(',', start);
           if (end < 0 || end >= res.Count()) return -1;
           string nonce = res.Substring(start + 8, end - start - 9);

           string password = hex_sha1("" + hex_sha1(hex_sha1(psw)) +servertime + nonce); //處理新浪微博用戶密碼psw


           string str ="entry=weibo&gateway=1&from=&savestate=7&useticket=1&ssosimplelogin=1&su="+
               uidbase64 + "&service=miniblog&servertime=" + servertime +"&nonce=" + nonce + "&pwencode=wsse&sp=" + password +"&encoding=utf-8&url=" +
               HttpUtility.UrlEncode("
           byte[] bytes;
           ASCIIEncoding encoding = new ASCIIEncoding();
           bytes = encoding.GetBytes(str);
           // bytes =System.Text.Encoding.UTF8.GetBytes(HttpUtility.UrlEncode(str));
           HttpWebRequest webRequest2 =(HttpWebRequest)WebRequest.Create("
           webRequest2.Method = "POST";
           webRequest2.ContentType = "application/x-www-form-urlencoded";

           webRequest2.ContentLength = bytes.Length;
           webRequest2.CookieContainer = cc;
           Stream stream;
           stream = webRequest2.GetRequestStream();
           stream.Write(bytes, 0, bytes.Length);
           stream.Close();

           HttpWebResponse response2 =(HttpWebResponse)webRequest2.GetResponse();
           StreamReader sr2 = new StreamReader(response2.GetResponseStream(),Encoding.Default);
           string res2 = sr2.ReadToEnd();
           int pos = res2.IndexOf("retcode");
           if (pos < 0 || pos > res2.Count()) return -1;
           int retcode = -1;
           for (pos += 8; pos < 100 + res2.Count(); pos++)
           {
               if (res2[pos] < '0' || res2[pos] > '9')
               {
                   retcode = 0;
                   break;
               }
               else if (res2[pos] > '0' && res2[pos] <= '9')
                   break;
           }
           if (retcode == -1) return -1;

           start = res2.IndexOf("location.replace");
           end = res2.IndexOf("")", start);
           url = res2.Substring(start + 18, end - start - 18);
           HttpWebRequest webRequest3 = (HttpWebRequest)WebRequest.Create(newUri(url));
           webRequest3.CookieContainer = cc;
           HttpWebResponse response3 =(HttpWebResponse)webRequest3.GetResponse();
           StreamReader sr3 = new StreamReader(response3.GetResponseStream(),Encoding.UTF8);
           res = sr3.ReadToEnd();


           foreach (Cookie cookie in response3.Cookies)
           {
               cc.Add(cookie);
           }
           return 0;
       }

 

 //base64加密
       public static string Base64Code(string Message)
       {
           char[] Base64Code = newchar[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T', 
        'U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n', 
        'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7', 
        '8','9','+','/','='};
           byte empty = (byte)0;
           System.Collections.ArrayList byteMessage = newSystem.Collections.ArrayList(System.Text.Encoding.Default.GetBytes(Message));
           System.Text.StringBuilder outmessage;
           int messageLen = byteMessage.Count;
           int page = messageLen / 3;
           int use = 0;
           if ((use = messageLen % 3) > 0)
           {
               for (int i = 0; i < 3 - use; i++)
                   byteMessage.Add(empty);
               page++;
           }
           outmessage = new System.Text.StringBuilder(page * 4);
           for (int i = 0; i < page; i++)
           {
               byte[] instr = new byte[3];
               instr[0] = (byte)byteMessage[i * 3];
               instr[1] = (byte)byteMessage[i * 3 + 1];
               instr[2] = (byte)byteMessage[i * 3 + 2];
               int[] outstr = new int[4];
               outstr[0] = instr[0] >> 2;
               outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >>4);
               if (!instr[1].Equals(empty))
                   outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >>6);
               else
                   outstr[2] = 64;
               if (!instr[2].Equals(empty))
                   outstr[3] = (instr[2] & 0x3f);
               else
                   outstr[3] = 64;
               outmessage.Append(Base64Code[outstr[0]]);
               outmessage.Append(Base64Code[outstr[1]]);
               outmessage.Append(Base64Code[outstr[2]]);
               outmessage.Append(Base64Code[outstr[3]]);
           }
           return outmessage.ToString();
       }

// sha-1加密

   public static stringhex_sha1(string Source_String)
       {
           byte[] StrRes = Encoding.Default.GetBytes(Source_String);
           HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
           StrRes = iSHA.ComputeHash(StrRes);
           StringBuilder EnText = new StringBuilder();
           foreach (byte iByte in StrRes)
           {
               EnText.AppendFormat("{0:x2}", iByte);
           }
           return EnText.ToString();
       }

 

 

打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
【new】新浪模擬登錄方式
模擬登錄看前端門外漢學習
模擬新浪微博登錄(Python+RSA加密算法)
模擬登錄-以新浪微博為例
微信分享接口
MessageDigest對密碼進行加密 - study - BlogJava
更多類似文章 >>
生活服務
熱點新聞
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服