|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Numerics;
using ZZ;
using System.Text.RegularExpressions;
public partial class Default6 : System.Web.UI.Page
{
static CookieContainer cc = new CookieContainer();
CookieContainer cookie = new CookieContainer();
protected void Page_Load(object sender, EventArgs e)
{
string uid = "新浪用户名";
string psw = "新浪密码";
int i = SinaLogin(uid,psw,cc);
//Response.Write(i);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://d.weibo.com/102803?current_page=1&since_id=&page=1"));
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string str = sr.ReadToEnd();
Response.Write(str);
}
public static int SinaLogin(string uid, string psw, CookieContainer cc)
{
string uidbase64 = Base64Code(uid);
//uidbase64 = uidbase64.Replace("=", @"%3D");
/*string url = "http://login.sina.com.cn/sso/prelogin.php?entry=miniblog&callback=sinaSSOController.preloginCallBack&user="
+ uidbase64 + "&client=ssologin.js(v1.3.16)&rsakt=mod";*/
string url = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su="+uidbase64+"&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.18)&_=1427853039234 ";
HttpWebRequest webRequest1 = (HttpWebRequest)WebRequest.Create(new Uri(url));
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);
start = res.IndexOf("pubkey");
if (start == -1) return -1;
end = res.IndexOf(',', start);
if (end == -1) return -1;
string pubkey = res.Substring(start + 9, end - start - 10);
start = res.IndexOf("rsakv");
if (start == -1) return -1;
end = res.IndexOf(',', start);
if (end == -1) return -1;
string rsakv = res.Substring(start + 8, end - start - 9);
//string password = hex_sha1("" + hex_sha1(hex_sha1(psw)) + servertime + nonce);//hex_sha1("" + hex_sha1(hex_sha1(psw)) + servertime + nonce)
string password=GetPwd(psw,servertime,nonce,pubkey);
byte[] bytes;
string str;
//RSASetPublic(pubkey, "10001");//1
//password = RSAEncrypt(servertime + "\t" + nonce + "\n" + psw); //1
/*string message = str(servertime) + '\t' + str(nonce) + '\n' + str(password)
rsaPublickey = int(rsapubkey, 16)
key = rsa.PublicKey(rsaPublickey, 65537)
passwd = rsa.encrypt(message, key)
passwd = binascii.b2a_hex(passwd)*/
//password=RSAEncrypt(pubkey,password);//3
//string password = "3b2cea1c187902d3a454e549c37c1c7cc3d503e572bd91ac5347aca50148b37bff11af033961a6c7d891192a23743f8169a9150907cd8253a921f4e3dd5d02be4e5c31f22e79c7a7e3ce91c372e6e109495c8338150cc1934a6b4b10ded2d61b9633884b1f4a8d5b3fbe5bd78e9304d334ac28408b6a9021871ac5746b4db8ce";
/*str = "entry=weibo&gateway=1&from=&savestate=7&useticket=1&vsnf=1&su=" + uidbase64 +
"&service=miniblog&servertime=" + servertime + "&nonce=" + nonce + "&pwencode=rsa2&rsakv=" + rsakv + "&sp=" + password
+ "&encoding=utf-8&prelt=34&url=" +
HttpUtility.UrlEncode("http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack") +
"&returntype=META";*/
str = "entry=weibo&gateway=1&from=&savestate=0&useticket=1&vsnf=1&su=" + uidbase64 + "&service=miniblog&servertime=" + servertime + "&nonce=" + nonce + "&pwencode=rsa2&rsakv="+rsakv+"&sp=" + password + "&sr=1440*900&encoding=UTF-8&prelt=654&url=" + HttpUtility.UrlEncode("http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack") + "&returntype=META";
ASCIIEncoding encoding = new ASCIIEncoding();
bytes = encoding.GetBytes(str);
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create("http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)");
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(new Uri(url));
webRequest3.CookieContainer = cc;
webRequest3.AllowAutoRedirect = false;
HttpWebResponse response3 = (HttpWebResponse)webRequest3.GetResponse();
StreamReader sr3 = new StreamReader(response3.GetResponseStream(), Encoding.UTF8);
res = sr3.ReadToEnd();
//Console.Write(res);
foreach (Cookie cookie in response3.Cookies)
{
cc.Add(cookie);
}
return 0;
}
public static string Base64Code(string Message)
{
char[] Base64Code = new char[]{'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 = new System.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();
}
}
模拟登陆的代码就是这样的,每次运行滚动加载的时候就会出现系统繁忙,然后一直处于正在加载中。。。。。,苏站长,你给看看
|
|