• [转][C#]HttpClient 代码示例


    转自:https://www.cnblogs.com/amosli/p/3918538.html

    也参考了:https://www.cnblogs.com/ShadowFiend007/p/8066855.html

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net.Http;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace ClassLibrary1
    {
        public class Class1
        {
            private static String dir = @"C:work";
    
            /// <summary>
            /// 写文件到本地
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="html"></param>
            public static void Write(string fileName, string html)
            {
                try
                {
                    FileStream fs = new FileStream(dir + fileName, FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                    sw.Write(html);
                    sw.Close();
                    fs.Close();
    
                }catch(Exception ex){
                    Console.WriteLine(ex.StackTrace);
                }
               
            }
    
            /// <summary>
            /// 写文件到本地
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="html"></param>
            public static void Write(string fileName, byte[] html)
            {
                try
                {
                    File.WriteAllBytes(dir + fileName, html);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                }
                
            }
    
            /// <summary>
            /// 登录博客园
            /// </summary>
            public static void LoginCnblogs()
            {
                HttpClient httpClient = new HttpClient();
                httpClient.MaxResponseContentBufferSize = 256000;
                httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
                String url = "http://passport.cnblogs.com/login.aspx";
                HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result;
                String result = response.Content.ReadAsStringAsync().Result;
    
                String username = "hi_amos";
                String password = "密码";
    
                do
                {
                    String __EVENTVALIDATION = new Regex("id="__EVENTVALIDATION" value="(.*?)"").Match(result).Groups[1].Value;
                    String __VIEWSTATE = new Regex("id="__VIEWSTATE" value="(.*?)"").Match(result).Groups[1].Value;
                    String LBD_VCID_c_login_logincaptcha = new Regex("id="LBD_VCID_c_login_logincaptcha" value="(.*?)"").Match(result).Groups[1].Value;
    
                    //图片验证码
                    url = "http://passport.cnblogs.com" + new Regex("id="c_login_logincaptcha_CaptchaImage" src="(.*?)"").Match(result).Groups[1].Value;
                    response = httpClient.GetAsync(new Uri(url)).Result;
                    Write("amosli.png", response.Content.ReadAsByteArrayAsync().Result);
                    
                    Console.WriteLine("输入图片验证码:");
                    String imgCode = "wupve";//验证码写到本地了,需要手动填写
                    imgCode =  Console.ReadLine();
    
                    //开始登录
                    url = "http://passport.cnblogs.com/login.aspx";
                    List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
                    paramList.Add(new KeyValuePair<string, string>("__EVENTTARGET", ""));
                    paramList.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", ""));
                    paramList.Add(new KeyValuePair<string, string>("__VIEWSTATE", __VIEWSTATE));
                    paramList.Add(new KeyValuePair<string, string>("__EVENTVALIDATION", __EVENTVALIDATION));
                    paramList.Add(new KeyValuePair<string, string>("tbUserName", username));
                    paramList.Add(new KeyValuePair<string, string>("tbPassword", password));
                    paramList.Add(new KeyValuePair<string, string>("LBD_VCID_c_login_logincaptcha", LBD_VCID_c_login_logincaptcha));
                    paramList.Add(new KeyValuePair<string, string>("LBD_BackWorkaround_c_login_logincaptcha", "1"));
                    paramList.Add(new KeyValuePair<string, string>("CaptchaCodeTextBox", imgCode));
                    paramList.Add(new KeyValuePair<string, string>("btnLogin", "登  录"));
                    paramList.Add(new KeyValuePair<string, string>("txtReturnUrl", "http://home.cnblogs.com/"));
                    response = httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result;
                    result = response.Content.ReadAsStringAsync().Result;
                    Write("myCnblogs.html",result);
                } while (result.Contains("验证码错误,麻烦您重新输入"));
    
                Console.WriteLine("登录成功!");
                
                //用完要记得释放
                httpClient.Dispose();
            }
    
            public static void Main()
            {
                  LoginCnblogs();
            }
    }

    经测试,代码也可以在 .net 4.0 下使用。

  • 相关阅读:
    LiLicense server OR Activation code
    一个比喻讲明Docker是什么
    Linux 系统目录结构说明
    Sublime Text2支持Vue语法高亮显示
    javascript权威指南笔记[6-8]
    javascript权威指南笔记[1-5]
    使用chrome控制台调试js代码
    windows与linux下执行.class(包含main方法)
    linux 命令
    几种常见的编码格式
  • 原文地址:https://www.cnblogs.com/z5337/p/9171386.html
Copyright © 2020-2023  润新知