版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、文章来源: HYPERLINK /1000.html 冷寒生学习笔记在C#中使用licurl来实现HTTP访问无论写什么程序,怎么少得了网络编程,以前为了实现一个程序中的采集功能,花了N多时间去学习socket,虽然对socket的三次握手能熟练运用。但最终仍以失败告终,主要是因为无法解决高频率进行socket连接时连接失败的问题,这个问题困扰了我二年左右。以前玩儿Linux的时候就用过curl这个开源程序,但当时只知道用curl来下载文件,却不知道可以在自己的程序中也能使用curl,这就是下面要说到的libcurl。本来有一个的开源程序,但自己用VC比用C#熟悉,所以就直接在VC中实现GET
2、/HEAD/POST这三个方法,然后生成DLL,再在C#中写个类来调用。 HYPERLINK /wp-content/uploads/2011/06/http.zip VC编写的libcurl动态链接库下载地址下面是在C#中学的调用类: HYPERLINK /27.html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
3、58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 14
4、3 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 21
5、8 219 220 221 222 223 224 225 226 227 228 229 230 231 using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Text.RegularExpressions; namespace zhidao class WinHttp private string m_url, m_referer, m_cookie, m_proxy, m_source, m_postdata
6、, m_accept, m_acceptlanguage, m_contenttype, m_useragent, m_head, m_location; private int m_port, m_statecode; public string Url protected getreturn m_url; setm_url = value; public string Referer protected get return m_referer; set m_referer = value; public string Cookie get return m_cookie; set m_c
7、ookie = value; public string Proxy protected get return m_proxy; set m_proxy = value; private string Source get return m_source; set m_source = value; public string PostData protected get return m_postdata; set m_postdata = value; public string Accept protected get return m_accept; set m_accept = va
8、lue; public string AcceptLanguage protected get return m_acceptlanguage; set m_acceptlanguage = value; public string ContentType protected get return m_contenttype; set m_contenttype = value; public string UserAgent protected get return m_useragent; set m_useragent = value; public string Head get re
9、turn m_head; protected set m_head = value; public string Location get return m_location; protected set m_location = value; public int Port protected get return m_port; set m_port = value; public int StateCode get return m_statecode; protected set m_statecode = value; /GET请求 DllImport(http.dll) priva
10、te static extern int get_url(StringBuilder url, StringBuilder referer, StringBuilder cookie, StringBuilder proxy, int proxy_port, StringBuilder source, int nsrc ); /HEAD请求 DllImport(http.dll) private static extern int get_head(StringBuilder url, StringBuilder referer, StringBuilder cookie, StringBui
11、lder proxy, int proxy_port, StringBuilder source, int nsrc); /POST请求 DllImport(http.dll) private static extern int post_url(StringBuilder url, StringBuilder referer, StringBuilder cookie, StringBuilder post_data, StringBuilder proxy, int proxy_port, StringBuilder source, int nsrc); /提取HTTP头中的各项数据 pr
12、ivate void HttpInfo() /从源码中提取HTTP头 int nPos = m_source.IndexOf(rnrn); if ( nPos = -1 ) return; m_head = m_source.Substring( 0, nPos ); /提取状态码 m_statecode = Convert.ToInt32( m_head.Substring( 9, 3 ) ); if ( m_statecode = 302 ) nPos = m_head.IndexOf( Location: ); if ( nPos != -1 ) m_location = m_head.
13、Substring( nPos, m_head.Length - nPos ); m_location = m_location.Substring( 0, m_location.IndexOf( rn ) ); /网页正文 nPos = m_source.IndexOf( rnrn ); m_source = m_source.Substring( nPos + 4, m_source.Length - nPos - 4); /提取Cookie string arr_tmp = Regex.Split(m_head, rn, RegexOptions.IgnoreCase); int ind
14、ex = 0; foreach ( string str_line in arr_tmp ) if (str_line.IndexOf(Set-Cookie: ) != -1) if (index != 0) m_cookie += ; ; string tmp = Set-Cookie: ; m_cookie += str_line.Substring( tmp.Length, str_line.Length - tmp.Length); index+; public string Get() StringBuilder HtmlSource = new StringBuilder(); H
15、tmlSource.Capacity = 204800; if (get_url( new StringBuilder(m_url), new StringBuilder(m_referer), new StringBuilder(m_cookie), new StringBuilder(m_proxy), m_port, HtmlSource, 204800) = 1) Source = HtmlSource.ToString(); /*提取Http头中的各项数据*/ HttpInfo(); return Source; public string GetHead() StringBuild
16、er HtmlSource = new StringBuilder(); HtmlSource.Capacity = 10240; if (get_head( new StringBuilder(m_url), new StringBuilder(m_referer), new StringBuilder(m_cookie), new StringBuilder(m_proxy), m_port, HtmlSource, 10240) = 1) Source = HtmlSource.ToString(); /*提取Http头中的各项数据*/ HttpInfo(); return Head; public string Post() StringBuilder HtmlSource = new StringBuilder(); HtmlSource.Capacity = 204800; if (post_url( new StringBuilder(m_url), new StringBuilder(m_referer), new StringBuilder(m_cookie), new StringB
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 制作考试题目与答案
- 2026年生活常识竞赛试题库
- 2026年护理伦理学知识测试卷
- 2026年生活科学知识竞赛题库
- 2026网络安全攻防技术与应用专项训练
- 2026年绿色生产与绿色消费习题集
- 2026年信息安全意识考核试题
- 2026年考研政治国际政治考点题库
- 2026年金融会计与审计实务操作模拟试题库
- 2026年道路安全知识与应用能力测试
- 掘进工作面安全风险辨识
- 颈椎病的康复上传版课件
- 人教版高一下学期期末考试数学试题与答案解析(共五套)
- 成人护理学绪论课件
- 2023-2024学年河南省开封市顺河区求实中学八年级(上)月考数学试卷(9月份)(含解析)
- 2024年《药物临床试验质量管理规范》(GCP)网络培训题库
- 内镜室半年工作总结
- 【模板】软件验证报告
- 广西科技大学家庭经济困难学生及家庭情况调查表、广西科技大学家庭经济困难学生认定申请表
- 《钛制锅具》规范
- QC成果提高冬季现浇混凝土施工一次合格率
评论
0/150
提交评论