转自: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. 2017.4.4 TCP/IP三次握手,四次挥手

    之前在电话面试的时候,被问到,所以找到一个超级容易理解的图片,自己保存,也算分享.

  2. linux 之sed

    sed 用法 sed [-nefr] [action] -i 直接修改文件内容,而不是像其他命令那样只是输出到终端 a新增c取代d删除i插入p列印常与sed -n 使用s取代 有一点需要注意的是:如果 ...

  3. redis服务以及phpredis扩展的安装

    一.下载软件包 下载redis wget http://download.redis.io/releases/redis-3.0.7.tar.gz 下载redis的php扩展 wget http:// ...

  4. 网络-console

    console接口h3c er8300cisco asaQuidway S5700-28C-SI Routing Switchtopsec <H3C>? reboot Reboot dev ...

  5. 深入详解美团点评CAT跨语言服务监控(四)服务端消息分发

    这边首先介绍下大众点评CAT消息分发大概的架构如下: 图4 消息分发架构图 分析管理器的初始化 我们在第一章讲到服务器将接收到的消息交给解码器(MessageDecoder)去做解码最后交给具体的消费 ...

  6. 基于MVC4+EasyUI的Web开发框架形成之旅(5)--框架总体界面介绍

    在前面介绍了一些关于最新基于MVC4+EasyUI的Web开发框架文章,虽然Web开发框架的相关技术文章会随着技术的探讨一直写下去,不过这个系列的文章,到这里做一个总结,展示一下整体基于MVC4+Ea ...

  7. RedHat无法ping通Win10的解决办法

    1.环境 主机: win10 企业版 64位 ip地址: 192.168.168.100 虚拟机:Red Hat ip地址: 192.168.168.200 2.现象: win10系统可以ping通R ...

  8. Vivado HLS初识---阅读《vivado design suite tutorial-high-level synthesis》(3)

    Vivado HLS初识---阅读<vivado design suite tutorial-high-level synthesis>(3) 优化lab1 1.创建工程,开启HLS 运行 ...

  9. python cntl使用

    import sys 2 import time 3 import fcntl 4 5 class FLOCK(object): 6 7 def __init__(self, name): 8 sel ...

  10. xe5 android 手机上使用sqlite [转]

    在android手机上怎样使用sqlite数据库,这里用Navigator实现 增删改查. 1.新建firemonkey mobile application 2.选择blank applicatio ...