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() {
正在阅读:
C# 操作http协议学习总结下10-07
综合布线习题及解答04-18
i.MX_6_G2D_API_User\\'s_Guide06-10
北卡罗来纳大学教堂山 University of North Carolina at Chapel07-09
XX市环保局招聘面试真题04-20
包含-"双"-字的成语07-18
修改梯形钢屋架设计书10-04
头雁工程学习心得03-18
七年级语文教学工作总结(新编版)06-05
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- C#
- 结下
- 协议
- 操作
- 学习
- http