转自: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 = ;
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[].Value;
String __VIEWSTATE = new Regex("id=\"__VIEWSTATE\" value=\"(.*?)\"").Match(result).Groups[].Value;
String LBD_VCID_c_login_logincaptcha = new Regex("id=\"LBD_VCID_c_login_logincaptcha\" value=\"(.*?)\"").Match(result).Groups[].Value; //图片验证码
url = "http://passport.cnblogs.com" + new Regex("id=\"c_login_logincaptcha_CaptchaImage\" src=\"(.*?)\"").Match(result).Groups[].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", ""));
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 下使用。

[转][C#]HttpClient 代码示例的更多相关文章

  1. 推荐Java五大微服务器及其代码示例教程

    来源素文宅博客:http://blog.yoodb.com/yoodb/article/detail/1339 微服务越来越多地用于开发领域,因为开发人员致力于创建更大,更复杂的应用程序,这些应用程序 ...

  2. Jsoup代码示例、解析网页+提取文本

    使用Jsoup解析HTML 那么我们就必须用到HttpClient先获取到html 同样我们引入HttpClient相关jar包 以及commonIO的jar包 我们把httpClient的基本代码写 ...

  3. 高级渲染技巧和代码示例 GPU Pro 7

    下载代码示例 移动设备正呈现着像素越来越高,屏幕尺寸越来越小的发展趋势. 由于像素着色的能耗非常大,因此 DPI 的增加以及移动设备固有的功耗受限环境为降低像素着色成本带来了巨大的压力. MSAA 有 ...

  4. Java8-Function使用及Groovy闭包的代码示例

    导航 定位 概述 代码示例 Java-Function Groovy闭包 定位 本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场 ...

  5. [IOS 开发] 懒加载 (延迟加载) 的基本方式,好处,代码示例

    懒加载的好处: 1> 不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强 2> 每个属性的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合 ...

  6. SELECT控件操作的JS代码示例

    SELECT控件操作的JS代码示例 1 检测是否有选中 if(objSelect.selectedIndex > -1) { //说明选中 } else { //说明没有选中 } 2.动态创建s ...

  7. 转:HIBERNATE一些_方法_@注解_代码示例---写的非常好

    HIBERNATE一些_方法_@注解_代码示例操作数据库7步骤 : 1 创建一个SessionFactory对象 2 创建Session对象 3 开启事务Transaction : hibernate ...

  8. Python实现各种排序算法的代码示例总结

    Python实现各种排序算法的代码示例总结 作者:Donald Knuth 字体:[增加 减小] 类型:转载 时间:2015-12-11我要评论 这篇文章主要介绍了Python实现各种排序算法的代码示 ...

  9. C#与数据库访问技术总结(十五)之 DataAdapter对象代码示例

    DataAdapter对象代码示例 下面的代码将说明如何利用DataAdapter对象填充DataSet对象. private static string strConnect=" data ...

随机推荐

  1. 2018.4.3 配置AD服务器步骤

    net ads命令表 配置AD服务器步骤:1. 安装rpm依赖包yum -y install pam_krb5* krb5-libs* krb5-workstation* krb5-devel* kr ...

  2. js中将一个字一个字的打印出来

    第一种方式: setTimeout(function(){ var cc=document.createTextNode(ss[i]) content.appendChild(cc) },3000)

  3. 企业库实现AOP的几种方法

    1.创建新对象时,分继承基类和继承接口 TargetClass theTarget = PolicyInjection.Create<TargetClass>(parameter1, pa ...

  4. python——SMTP发送简单邮件

    [root@localhost python]# cat smtp.py import smtplib import string from email.mime.text import MIMETe ...

  5. Centos7安装32位库用来安装32位软件程序

    打算在centos7上安装一个远程控制软件,发现teamviewer只有centos的32位版本,试着安装了一下,各种报错,尝试多次无果.于是试着用向日葵远程控制软件,也报错,貌似也是只支持32位. ...

  6. 10 Rules of Highly Successful Project Management

    I commited the information below to report PDU of PMI. ^_^. In this paper, the author introduces his ...

  7. TS学习之for..of

    for..of会遍历可迭代的对象,调用对象上的Symbol.iterator方法(可迭代对象,数组,字符串等) let arr = ["hello", "ts" ...

  8. mysql中 where in 用法详解

    这里分两种情况来介绍 1.in 后面是记录集,如: select  *  from  table  where   uname  in(select  uname  from  user); 2.in ...

  9. shutdown和close

    close close 一个套接字的默认行为是把套接字标记为已关闭,然后立即返回到调用进程,该套接字描述符不能再由调用进程使用,也就是说它不能再作为read或write的第一个参数,然而TCP将尝试发 ...

  10. MySQL聚合函数与数据分组

    我们最常需要的是汇总数据而不是把他们实际检索出来 确定表中行数(或满足某个条件或包含某个特定值的行数) 确定表中行组的和 找出表列(或所有行或特定列)的最大值,最小值和平均值 聚集函数是运行在行组上, ...