HttpClient 应用案例揭破应用Discuss论坛登录
闲来无事,写了一个对discuss论坛登录的案例,初次上场按照以前的惯例没成功,见过抓包分析discuss论坛成功完成,废话不多说 直接上代码。
1:winform 做客户端,添加HttpClient的引用
初始化对象:
private const string BaseUrl = "替换为论坛地址"; private const string useAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"; private readonly HttpClient httpClient; private readonly HttpClientHandler handler; private readonly string userName = "superadmin"; private readonly string passWord = "123456"; private readonly HtmlDocument htmlDoc; private BaiDuToken baiDuToken; public Form1() { InitializeComponent(); handler = new HttpClientHandler() { UseCookies = true, AllowAutoRedirect = true }; httpClient = new HttpClient(handler) { BaseAddress = new Uri(BaseUrl) }; httpClient.DefaultRequestHeaders.Add("user-agent", useAgent); httpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive"); httpClient.DefaultRequestHeaders.Add("Keep-Alive", "timeout=600"); httpClient.DefaultRequestHeaders.Add("Referer", $"{BaseUrl}member.php?mod=logging&action=login"); htmlDoc = new HtmlDocument(); }
2:具体方法
/// <summary> /// 获取导航 /// </summary> /// <param name="fid"></param> /// <returns></returns> private string GetNav(int fid = 72) { return $"forum.php?mod=forumdisplay&fid={fid}"; } /// <summary> /// 百度ai token /// </summary> /// <returns></returns> private async Task<BaiDuToken> BaiDuTokenAsync() => await httpClient.GetAccessTokenAsync(); //百度ai 代码 自我实现 /// <summary> /// 搜索字符 /// </summary> /// <param name="allStr"></param> /// <param name="firstStr"></param> /// <param name="lastStr"></param> /// <returns></returns> public string GetStringMid(string allStr, string firstStr, string lastStr) { int num = allStr.IndexOf(firstStr); int num2 = allStr.IndexOf(lastStr, num + firstStr.Length); if (num < 0 || num2 < 0) { return ""; } num += firstStr.Length; num2 -= num; if (num < 0 || num2 < 0) { return ""; } return allStr.Substring(num, num2); } /// <summary> /// 搜索字符 /// </summary> /// <param name="allStr"></param> /// <param name="firstStr"></param> /// <param name="lastStr"></param> /// <param name="regexCode"></param> /// <returns></returns> public List<string> GetStringMids(string allStr, string firstStr, string lastStr, string regexCode = "(.*?)") { List<string> list = new List<string>(); string pattern = $"{firstStr}{regexCode}{lastStr}"; Regex regex = new Regex(pattern); MatchCollection matchCollection = regex.Matches(allStr); for (int i = 0; i < matchCollection.Count; i++) { GroupCollection groups = matchCollection[i].Groups; for (int j = 1; j < groups.Count; j++) { string value = groups[j].Value; if (!string.IsNullOrEmpty(value)) { list.Add(value); } } } return list; } /// <summary> /// 响应html为字符串 /// </summary> /// <param name="url"></param> /// <returns></returns> private async Task<string> GetResponseStrAsync(string url) { return await (await GetResponseAsync(url))?.ReadAsStringAsync(); } /// <summary> /// 响应 /// </summary> /// <param name="url"></param> /// <returns></returns> private async Task<HttpContent> GetResponseAsync(string url) { HttpResponseMessage response = await httpClient.GetAsync(url); if (!response.IsSuccessStatusCode) { return null; } return response.Content;//具体结果 } private string seccode, loginhash, formhash; /// <summary> /// 获取登录相关参数 /// </summary> /// <returns></returns> private async Task GetformAsync() //获取登录相关参数 { string url = "member.php?mod=logging&action=login"; string html = await GetResponseStrAsync(url); seccode = GetStringMid(html, "seccode_", "\""); loginhash = GetStringMid(html, "loginhash=", "\""); formhash = GetStringMid(html, "<input type=\"hidden\" name=\"formhash\" value=\"", "\""); } /// <summary> /// 获取验证码地址 /// </summary> /// <returns></returns> private async Task<string> GetupdateAsync()//获取验证码地址 { string url = $"misc.php?mod=seccode&action=update&idhash={seccode}";//请求地址 string res = await GetResponseStrAsync(url); return GetStringMid(res, "width=\"100\" height=\"30\" src=\"", "\""); } private async void PictureBox1_Click(object sender, EventArgs e) { await GetImageAsync(); } private async void Form1_Load(object sender, EventArgs e) { await GetformAsync(); await GetImageAsync(); baiDuToken = await BaiDuTokenAsync(); } /// <summary> /// 获取验证码图片 /// </summary> /// <param name="update"></param> /// <returns></returns> private async Task<byte[]> GetImageAsync() { string update = await GetupdateAsync(); byte[] bytes = await httpClient.GetByteArrayAsync(update); return bytes; } /// <summary> /// 登录 /// </summary> /// <returns></returns> private async Task<bool> GetLoginAsync() { byte[] bytes = await GetImageAsync(); ImagToWords imagToWords = BaiDuAccessToken.RecogniseImage(bytes, baiDuToken);//利用百度 ai识别验证码 string code = string.Join("", imagToWords.Words_result.Select(s => s.Words)); string url = $"misc.php?mod=seccode&action=check&inajax=1&modid=member::logging&idhash={seccode}&secverify={code}";//请求地址 string html = await GetResponseStrAsync(url); if (html.Contains("succeed")) { url = $"member.php?mod=logging&action=login&loginsubmit=yes&loginhash{loginhash}&inajax=1";//请求地址 List<KeyValuePair<string, string>> paraList = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("formhash", formhash), new KeyValuePair<string, string>("username", userName), new KeyValuePair<string, string>("password", passWord), new KeyValuePair<string, string>("seccodehash", seccode), new KeyValuePair<string, string>("seccodeverify", code), }; HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(paraList)); html = await response.Content.ReadAsStringAsync(); return html.Contains("欢迎您回来"); } return false; }