C# 操作http协议学习总结下

更新时间:2023-10-07 16:58:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

C# 中使用System.Net.Http.HttpClient 模拟登录博客园 (GET/POST)

主题 HttpComponents C#

一、 System.Net.Http .HttpClient简介

System.Net.Http 是微软.net4.5中推出的HTTP 应用程序的编程接口, 微软称之为“现代化的 HTTP 编程接口”, 主要提供如下内容: 1. 用户通过 HTTP 使用现代化的 Web Service 的客户端组件;

2. 能够同时在客户端与服务端同时使用的 HTTP 组件(比如处理 HTTP 标头和消息), 为客户端和服务端提供一致的编程模型。

个人看来是抄袭 apache http client ,目前网上用的人好像不多, 个人认为使用httpclient最大的好处是:不用自己管理cookie,只要负责写好请求即可。 由于网上资料不多,这里借登录博客园网站做个简单的总结其get和post请求的用法。

查看微软的api可以发现其属性方法:

http://msdn.microsoft.com/zh-cn/library/system.net.http.httpclient.aspx 由其api可以看出如果想 设置请求头 只需要在 DefaultRequestHeaders 里进行设置

创建httpcliet可以直接new HttpClient()

发送请求 可以按发送方式分别调用其方法,如 get调用 GetAsync(Uri) , post调用 PostAsync(Uri, HttpContent) ,其它依此类推。。。 二、实例(模拟post登录博客园)

首先,需要说明的是, 本实例环境是win7 64位+vs 2013+ .net 4.5框架。 1.使用vs2013新建一个控制台程序,或者窗体程序,如下图所示:

2.必须引入System.Net.Http框架,否则将不能使用httpclient 3.实现代码

border-box;\border-box;\string=\border-box;\string=\border-box;\string=\border-box;\string=\

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 = @\ /// /// 写文件到本地 /// /// /// 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); } } /// /// 写文件到本地 /// /// /// public static void Write(string fileName, byte[] html) { try { File.WriteAllBytes(dir + fileName, html); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } } /// /// 登录博客园 /// public static void LoginCnblogs() { HttpClient httpClient = new HttpClient(); httpClient.MaxResponseContentBufferSize = 256000; httpClient.DefaultRequestHeaders.Add(\la/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36\String url = \HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result; String result = response.Content.ReadAsStringAsync().Result; String username = \String password = \密码\ do { String __EVENTVALIDATION = new Regex(\ION\\\String __VIEWSTATE = new Regex(\\\\String LBD_VCID_c_login_logincaptcha = new Regex(\D_VCID_c_login_logincaptcha\\%ult).Groups[1].Value; //图片验证码 url = \ogin_logincaptcha_CaptchaImage\\\sult).Groups[1].Value; response = httpClient.GetAsync(new Uri(url)).Result; Write(\().Result); Console.WriteLine(\输入图片验证码:\String imgCode = \验证码写到本地了,需要手动填写 imgCode = Console.ReadLine(); //开始登录 url = \List> paramList = new List>(); paramList.Add(new KeyValuePair(\paramList.Add(new KeyValuePair(\paramList.Add(new KeyValuePair(\E)); paramList.Add(new KeyValuePair(\NTVALIDATION)); paramList.Add(new KeyValuePair(\paramList.Add(new KeyValuePair(\paramList.Add(new KeyValuePair(\tcha\paramList.Add(new KeyValuePair(\n_logincaptcha\paramList.Add(new KeyValuePair(\ode)); paramList.Add(new KeyValuePair(\登 录\paramList.Add(new KeyValuePair(\me.cnblogs.com/\response = httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result; result = response.Content.ReadAsStringAsync().Result; Write(\} while (result.Contains(\验证码错误,麻烦您重新输入\ Console.WriteLine(\登录成功!\ //用完要记得释放 httpClient.Dispose(); } public static void Main() {

本文来源:https://www.bwwdw.com/article/cf3d.html

Top