c#協議登錄空間解析

項目背景:

近期有朋友想讓我幫他實現一個自動登錄qq空間,並實現自動點贊,評論,留言之類的功能。正好閒來無事,研究一下實現方法。

實現方法:

要實現此功能其實很簡單,無非就是登錄成功後獲取到cookie,然後通過get、post方式請求功能接口。

大致思路:

  1. get方式請求獲取登錄二維碼接口,並拿到二維碼圖片(這裡為什麼要選擇用二維碼方式登錄呢,因為掃碼方式最簡單,賬號密碼登錄時會有滑塊驗證,有時還會有設備鎖驗證)
  2. 每隔1秒或2秒調用接口來監聽二維碼狀態
  3. 當監聽到登錄成功後取出cookie,此時響應裡有一個url,我們把它取出來並跳轉到這個路徑(需要禁止重定向)

好了,介紹完畢,下面來實際操作吧

新建一個winform程序,在窗體上放一個PictureBox控件用來展示二維碼

c#協議登錄空間解析

在窗體加載事件中,我們來獲取二維碼

<code>private void Form1_Load(object sender, EventArgs e)
{
//1、GET方式請求二維碼圖片
HttpWebRequest request = HttpWebRequest.CreateHttp("https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&e=2&l=M&s=3&d=72&v=4&t=0.6676227813796067&daid=5&pt_3rd_aid=0");
request.Method = RequestMethod.GET;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36";
//2、得到響應消息
WebResponse response = request.GetResponse();
//3、拿到響應消息中的頭部信息中的Set-Cookie值(用於計算ptqrtoken)
WebHeaderCollection headerCollection = response.Headers;
Cookie.cookie1 = headerCollection.Get("set-cookie");
//4、拿到二維碼圖片,然後把這個圖片展示在窗體中
Stream stream = response.GetResponseStream();
Image image = Image.FromStream(stream);
//5、展示在窗體中
login_qrcode.Image = image;

\t\t\t\t//異步監聽二維碼狀態
Task task = new Task(() =>
{
while (true)
{
if (ListenLoginStatus())
{
break;
}
Thread.Sleep(1000);
}
});
task.Start();
}/<code>

二維碼獲取成功後,需要監聽二維碼狀態(共四個狀態:二維碼未失效、掃描未確定登錄、已確定登錄、二維碼已失效)

用戶掃描窗體上的二維碼圖片,並確認登錄後,就停止監聽二維碼並得到登錄成功的cookie,拿到cookie後,就可以進行一系列的操作了,比如。。。再比如。。。自己想去吧

<code>\t\t\t\t/// <summary>
/// 監聽二維碼狀態 登錄成功返回true
/// /<summary>
public bool ListenLoginStatus()
{
//1、計算出必要的參數ptqrtoken
/////// 這裡的cookie1值是獲取二維碼接口響應體中的set-cookie
string ptqrtoken = ptqrtoken(Cookie.cookie1.Split(';')[0].Split('=')[1]);
//2、請求二維碼狀態
string url = "https://ssl.ptlogin2.qq.com/ptqrlogin";
url += "?u1=https://qzs.qq.com/qzone/v5/loginsucc.html?para=izone";
url += "&ptqrtoken=" + ptqrtoken;
url += "&ptredirect=0";
url += "&h=1";
url += "&t=1";
url += "&g=1";
url += "&from_ui=1";
url += "&ptlang=2052";
url += "&action=0-0-" + DateUtil.GetTimeStamp2(); // 獲取當前時間戳(毫秒級)
url += "&js_ver=20021917";
url += "&js_type=1";
url += "&login_sig=";
url += "&pt_uistyle=40";
url += "&aid=549000912";
url += "&daid=5";
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.Method = RequestMethod.GET;
request.Headers.Add("cookie", Cookie.cookie1);
WebResponse response = request.GetResponse();
string result = "";
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
result = reader.ReadToEnd().ToString();

}
if (!(result.Contains("http") && result.Contains("登錄成功")))
return false;

//登錄成功後,取出用戶暱稱
string[] arrayResult = result.Replace("ptuiCB(", "").TrimEnd(')').Split(',');
UserNickName = arrayResult[arrayResult.Length - 1].Trim().Trim('\\'');

//登錄成功後,取出cookie
Cookie.cookie2 = response.Headers.Get("set-cookie");

//掃碼登錄成功後,會返回一個路徑,跳轉到這個路徑後,取出cookie
url = arrayResult[2].Trim().Trim('\\'');
request = HttpWebRequest.CreateHttp(url);
request.Method = RequestMethod.GET;
//必須禁用重定向,否則會自動重定向到別的網址,就無法取到cookie
request.AllowAutoRedirect = false;
response = request.GetResponse();
Cookie.cookie3 = response.Headers.Get("set-cookie");

//取出登錄成功的qq號碼
string[] _a = url.Split('&');
foreach (string item in _a)
{
if (item.IndexOf("uin=") == 0)
{
UserQqNumber = item.Replace("uin=", "");
break;
}
}

//訪問個人中心
request = HttpWebRequest.CreateHttp("https://user.qzone.qq.com/" + UserQqNumber);
request.Method = RequestMethod.GET;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36";
//p_skey用於計算g_tk
string p_skey = "";
string[] arrayCookie = Cookie.cookie3.Replace(";,", ";").TrimEnd(';').Split(';');
foreach (string item in arrayCookie)
{
if (item.IndexOf("uin=") == 0)

Cookie.cookie4 += item + "; ";
if (item.IndexOf("skey=") == 0)
Cookie.cookie4 += item + "; ";
if (item.IndexOf("p_uin=") == 0)
Cookie.cookie4 += item + "; ";
if (item.IndexOf("p_skey=") == 0)
{
Cookie.cookie4 += item + "; ";
if (!(item.IndexOf("p_skey_forbid") == 0) && !item.Equals("p_skey="))
p_skey = item.Replace("p_skey=", "");
}
}
if (!string.IsNullOrWhiteSpace(Cookie.cookie4))
Cookie.cookie4 = Cookie.cookie4.Trim().TrimEnd(';');
request.Headers.Add(Header.cookie, Cookie.cookie4);
response = request.GetResponse();

return true;
}

/// <summary>
/// 計算ptqrtoken
/// 用於監聽二維碼狀態
/// /<summary>
public string ptqrtoken(string qrsign)
{
int e = 0;
for (int i = 0; i < qrsign.Length; ++i)
{
e += (e << 5) + (int)(char)(qrsign[i]);
}
return e+ "";
}/<code>

這邊文章到這裡就結束了,主要介紹瞭如何實現通過掃描二維碼來登錄qq空間,後續的別的操作(點贊評論)下次再說


分享到:


相關文章: