1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Register
  6. {
  7. public class HttpClass
  8. {
  9. private System.Net.HttpWebRequest Request = null;
  10. private System.Net.WebResponse Response = null;
  11. private System.IO.Stream Stream = null;
  12. private System.IO.StreamReader Read = null;
  13. private System.Byte[] Byte = null;
  14. private System.Text.Encoding Encode = System.Text.Encoding.UTF8;
  15. private System.Text.RegularExpressions.Match Match = null;
  16. /// <summary>
  17. /// 公开属性
  18. /// </summary>
  19. public System.Net.WebProxy Proxy = new System.Net.WebProxy()
  20. public string IPFor = null;
  21. public bool HideInfo = false;
  22. /// <summary>
  23. /// 初始化Request
  24. /// </summary>
  25. /// <param name="Request">对象</param>
  26. private void init_Request(ref System.Net.HttpWebRequest Request)
  27. {
  28. //终端信息
  29. Request.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,image/png,application/java-archive,application/java,application/x-java-archive,text/vnd.sun.j2me.app-descriptor,application/vnd.oma.drm.message,application/vnd.oma.drm.content,application/vnd.oma.dd+xml,application/vnd.oma.drm.rights+xml,application/vnd.oma.drm.rights+wbxml,application/x-nokia-widget,text/x-opml,application/vnd.nokia.headwrapper,*/*;q=0.5";
  30. Request.Headers.Add("Accept-Charset", "iso-8859-1,utf-8;q=0.7,*;q=0.7");
  31. Request.Headers.Add("Accept-Language", "zh-cn, zh;q=1.0,en;q=0.5,en;q=0.5,en;q=0.5");
  32. Request.Headers.Add("Accept-Encoding", "gzip, deflate, x-gzip, identity; q=0.9");
  33. Request.Headers.Add("X-Nokia-MusicShop-Version", "11.0842.9");
  34. //承载方式
  35. Request.Headers.Add("X-Nokia-MusicShop-Bearer", "GPRS/3G");
  36. Request.Headers.Add("X-Nokia-CONNECTION_MODE", "TCP");
  37. Request.Headers.Add("X-Nokia-BEARER", "3G");
  38. Request.Headers.Add("x-up-bear-type", "GPRS/EDGE");
  39. Request.Headers.Add("X-Up-Bearer-Type", "GPRS");
  40. //GGSN信息
  41. Request.Headers.Add("x-source-id", "GZGGSN1101BEr");
  42. Request.Headers.Add("Client-IP", "211.139.151.74");
  43. if (IPFor != null) Request.Headers.Add("x-forwarded-for", IPFor);
  44. //网关信息
  45. Request.Headers.Add("Via", "WTP/1.1 192.168.13.33 (Nokia WAP Gateway 4.1/CD1/ECD13_E/4.1.05)");
  46. Request.Headers.Add("X-Nokia-gateway-id", "NWG/4.1/Build4.1.05");
  47. //目标主机
  48. Request.Headers.Add("X-Online-Host", Request.Host);
  49. //重要信息
  50. if (!HideInfo)
  51. {
  52. Request.UserAgent = "Mozilla/5.0 (SymbianOS/9.4; U; Series60/5.0 Nokia5230/10.0.055; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413";
  53. Request.Headers.Add("x-wap-profile", "http://nds1.nds.nokia.com/uaprof/Nokia5230r100-3G.xml")
  54. if (IPFor != null)
  55. {
  56. Request.Headers.Add("x-nokia-ipaddress", IPFor);
  57. }
  58. }
  59. //自动重定向
  60. Request.AllowAutoRedirect = false;
  61. //代理设置
  62. if (Proxy.Address != null)
  63. {
  64. Request.Proxy = Proxy;
  65. }
  66. //其它杂项
  67. Request.KeepAlive = false;
  68. Request.Timeout = 8000;
  69. }
  70. //         public void GZipDecompress(ref System.IO.Stream refStream)
  71. //         {
  72. //             using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
  73. //             {
  74. //                 using (System.IO.Compression.GZipStream gZip = new System.IO.Compression.GZipStream(refStream, System.IO.Compression.CompressionMode.Decompress, true))
  75. //                 {
  76. //                     System.IO.BinaryReader reader = new System.IO.BinaryReader(gZip);
  77. //                     System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
  78. //                     while (true)
  79. //                     {
  80. //                         char[] p = reader.ReadChars(32);
  81. //                         byte[] buffer = reader.ReadBytes(32);
  82. //                         if (buffer == null || buffer.Length < 1) break;
  83. //                         writer.Write(buffer);
  84. //                     }
  85. //                     writer.Close();
  86. //                     gZip.CopyTo(stream);
  87. //                 }
  88. //                 stream.CopyTo(refStream);
  89. //             }
  90. //         }
  91. /// <summary>
  92. /// 获取网页数据
  93. /// </summary>
  94. /// <param name="url">请求地址</param>
  95. /// <returns>失败返回null</returns>
  96. public string get_Internet(string url, string cookie = null)
  97. {
  98. try
  99. {
  100. Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
  101. url = null;
  102. if (Request != null)
  103. {
  104. init_Request(ref Request);
  105. if (cookie != null) { Request.Headers.Add("Cookie", cookie); }
  106. using (Response = Request.GetResponse())
  107. {
  108. Stream = Response.GetResponseStream();
  109. using (Read = new System.IO.StreamReader(Stream, System.Text.Encoding.UTF8))
  110. {
  111. url = Read.ReadToEnd();
  112. }
  113. Read = null;
  114. Stream = null;
  115. }
  116. Response = null;
  117. Request.Abort();
  118. Request = null;
  119. }
  120. return url;
  121. }
  122. catch(Exception ex)
  123. {
  124. Error.Write(ref ex);
  125. return null;
  126. }
  127. }
  128. /// <summary>
  129. /// 获取数据流
  130. /// </summary>
  131. /// <param name="url">地址</param>
  132. /// <param name="Stream">返回数据流</param>
  133. /// <returns>失败为false</returns>
  134. public bool get_Stream(string url, ref System.IO.MemoryStream Stream, ref string cookie)
  135. {
  136. try
  137. {
  138. Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
  139. init_Request(ref Request);
  140. if (cookie != null) { Request.Headers.Add("Cookie", cookie); }
  141. using (Response = Request.GetResponse())
  142. {
  143. Response.GetResponseStream().CopyTo(Stream);
  144. if (cookie != null)
  145. {
  146. cookie += " " + get_Match(Response.Headers.Get("Set-Cookie"), "verifysession=([^;]+);").Replace(";", "");
  147. }
  148. }
  149. Response = null;
  150. }
  151. catch (Exception ex)
  152. {
  153. Error.Write(ref ex);
  154. return false;
  155. }
  156. if (Request != null)
  157. {
  158. Request.Abort();
  159. Request = null;
  160. }
  161. return true;
  162. }
  163. /// <summary>
  164. /// 提交数据并获取返回数据
  165. /// </summary>
  166. /// <param name="url">请求地址</param>
  167. /// <returns>失败返回null</returns>
  168. public string post_Internet(string url, string data, string cookie = null)
  169. {
  170. try
  171. {
  172. Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
  173. url = null;
  174. if (Request != null)
  175. {
  176. init_Request(ref Request);
  177. if (cookie != null) { Request.Headers.Add("Cookie", cookie); }
  178. Request.Method = "POST";
  179. Request.ServicePoint.Expect100Continue = false;
  180. Request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
  181. Byte = Encode.GetBytes(data);
  182. Request.ContentLength = Byte.Length;
  183. using (Stream = Request.GetRequestStream())
  184. {
  185. Stream.Write(Byte, 0, Byte.Length);
  186. }
  187. Stream = null;
  188. Byte = null;
  189. data = null;
  190. using (Response = Request.GetResponse())
  191. {
  192. Stream = Response.GetResponseStream();
  193. using (Read = new System.IO.StreamReader(Stream))
  194. {
  195. url = Read.ReadToEnd();
  196. }
  197. Read = null;
  198. Stream = null;
  199. }
  200. Response = null;
  201. Request.Abort();
  202. Request = null;
  203. }
  204. return url;
  205. }
  206. catch (Exception ex)
  207. {
  208. Error.Write(ref ex);
  209. return null;
  210. }
  211. }
  212. /// <summary>
  213. /// 获取服务器响应报文信息
  214. /// </summary>
  215. /// <param name="url">请求地址</param>
  216. /// <returns>HTTP响应报文</returns>
  217. public string get_All(string url, string cookie = null)
  218. {
  219. try
  220. {
  221. Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
  222. url = null;
  223. if (Request != null)
  224. {
  225. Request.Method = "GET";
  226. init_Request(ref Request);
  227. if (cookie != null) { Request.Headers.Add("Cookie", cookie); }
  228. using (Response = Request.GetResponse())
  229. {
  230. foreach (string name in Response.Headers.AllKeys)
  231. {
  232. url += name + "=" + Response.Headers.Get(name) + "; ";
  233. }
  234. Stream = Response.GetResponseStream();
  235. using (Read = new System.IO.StreamReader(Stream, System.Text.Encoding.UTF8))
  236. {
  237. url += Read.ReadToEnd();
  238. }
  239. Read = null;
  240. Stream = null;
  241. }
  242. Response = null;
  243. Request.Abort();
  244. Request = null;
  245. }
  246. return url;
  247. }
  248. catch (Exception ex)
  249. {
  250. Error.Write(ref ex);
  251. return null;
  252. }
  253. }
  254. /// <summary>
  255. /// 获取服务器返回信息
  256. /// </summary>
  257. /// <param name="url">请求地址</param>
  258. /// <returns>HTTP Header</returns>
  259. public string get_Header(string url, string key = "Set-Cookie", string cookie = null)
  260. {
  261. try
  262. {
  263. Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
  264. url = null;
  265. if (Request != null)
  266. {
  267. Request.Method = "HEAD";
  268. init_Request(ref Request);
  269. if (cookie != null) { Request.Headers.Add("Cookie", cookie); }
  270. using (Response = Request.GetResponse())
  271. {
  272. url = Response.Headers.Get(key);
  273. }
  274. Response = null;
  275. Request.Abort();
  276. Request = null;
  277. }
  278. return url;
  279. }
  280. catch (Exception ex)
  281. {
  282. Error.Write(ref ex);
  283. return null;
  284. }
  285. }
  286. /// <summary>
  287. /// 获取匹配信息
  288. /// </summary>
  289. /// <param name="src">匹配内容</param>
  290. /// <returns>正则表达式</returns>
  291. public string get_Match(string src, string pattern, string substr = null)
  292. {
  293. Match = System.Text.RegularExpressions.Regex.Match(src, pattern);
  294. if (!Match.Success)
  295. {
  296. return null;
  297. }
  298. if (substr != null)
  299. {
  300. return Match.Groups[substr].Value;
  301. }
  302. return Match.Value;
  303. }
  304. /// <summary>
  305. /// 发送邮件
  306. /// </summary>
  307. /// <param name="ToAddress"></param>
  308. /// <param name="Subject"></param>
  309. /// <param name="Value"></param>
  310. /// <param name="PathArray"></param>
  311. /// <returns></returns>
  312. public static bool try_Email(string ToAddress, string Subject, string Value, string[] PathArray = null)
  313. {
  314. bool result = false;
  315. System.Net.Mail.SmtpClient Mail = new System.Net.Mail.SmtpClient("smtp.qq.com", 25);
  316. Mail.UseDefaultCredentials = true;
  317. Mail.Credentials = new System.Net.NetworkCredential("feedback01", "z123456");
  318. Mail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
  319. System.Net.Mail.MailMessage Messages = new System.Net.Mail.MailMessage(new System.Net.Mail.MailAddress("feedback01@qq.com"), new System.Net.Mail.MailAddress(ToAddress));
  320. Messages.BodyEncoding = System.Text.Encoding.UTF8;
  321. Messages.SubjectEncoding = System.Text.Encoding.UTF8;
  322. Messages.Subject = Subject;
  323. Messages.Body = Value;
  324. System.Net.Mail.Attachment[] Attachments = null;
  325. try
  326. {
  327. if (PathArray != null)
  328. {
  329. Attachments = new System.Net.Mail.Attachment[PathArray.Length];
  330. for (int i = 0; i < PathArray.Length; i++)
  331. {
  332. if (PathArray[i].Length >= 6 && System.IO.File.Exists(PathArray[i]))
  333. {
  334. Attachments[i] = new System.Net.Mail.Attachment(PathArray[i]);
  335. Messages.Attachments.Add(Attachments[i]);
  336. }
  337. }
  338. }
  339. Mail.Send(Messages);
  340. result = true;
  341. }
  342. catch(Exception ex)
  343. {
  344. Error.Write(ref ex);
  345. result = false;
  346. }
  347. if (Attachments != null)
  348. {
  349. for (int k = 0; k < Attachments.Length; k++)
  350. {
  351. if (Attachments[k] != null)
  352. {
  353. Attachments[k].Dispose();
  354. }
  355. }
  356. }
  357. Messages.Dispose();
  358. Mail.Dispose();
  359. return result;
  360. }
  361. /// <summary>
  362. /// 用于ShellExecuteEx
  363. /// </summary>
  364. [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
  365. public struct SHELLEXECUTEINFO
  366. {
  367. public int cbSize;
  368. public int fMask;
  369. public int hwnd;
  370. [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
  371. public string lpVerb;
  372. [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
  373. public string lpFile;
  374. [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
  375. public string lpParameters;
  376. public int lpDirectory;
  377. public int nShow;
  378. public int hInstApp;
  379. public int lpIDList;
  380. public int lpClass;
  381. public int hkeyClass;
  382. public int dwHotKey;
  383. public int hIcon;
  384. public int hProcess;
  385. }
  386. /// <summary>
  387. /// ShellExecuteEx
  388. /// </summary>
  389. /// <param name="lpExecInfo"></param>
  390. /// <returns></returns>
  391. [System.Runtime.InteropServices.DllImport("Shell32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  392. public static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
  393. static SHELLEXECUTEINFO ShellInfo;
  394. /// <summary>
  395. /// 打开URL
  396. /// </summary>
  397. /// <param name="url"></param>
  398. public static void OpenURL(string url)
  399. {
  400. if (ShellInfo.lpFile == null)
  401. {
  402. Microsoft.Win32.RegistryKey SubKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command");
  403. if (SubKey == null)
  404. {
  405. return;
  406. }
  407. string Explorer = ((string)SubKey.GetValue(null)).Replace("\"", "");
  408. int SpaceOffset = -1;
  409. if ((SpaceOffset = Explorer.LastIndexOf(" ")) != -1)
  410. {
  411. Explorer = Explorer.Substring(0, SpaceOffset);
  412. }
  413. ShellInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(ShellInfo);
  414. ShellInfo.lpFile = Explorer;
  415. ShellInfo.nShow = 1; //SW_SHOWNORMAL
  416. SubKey.Close();
  417. SubKey.Dispose();
  418. }
  419. ShellInfo.lpParameters = url;
  420. ShellExecuteEx(ref ShellInfo);
  421. }
  422. }
  423. }

C# HTTP网络常用方法封装的更多相关文章

  1. java Map常用方法封装

      java Map常用方法封装 CreationTime--2018年7月16日15点59分 Author:Marydon 1.准备工作 import java.util.HashMap; impo ...

  2. 上门洗车APP --- Androidclient开发 之 网络框架封装介绍(二)

    上门洗车APP --- Androidclient开发 之 网络框架封装介绍(二) 前几篇博文中给大家介绍了一下APP中的基本业务及开发本项目使用的网络架构: 上门洗车APP --- Androidc ...

  3. Android OkHttp网络连接封装工具类

    package com.lidong.demo.utils; import android.os.Handler; import android.os.Looper; import com.googl ...

  4. React-Native 之 GD (八)GET 网络请求封装

    1.到这里,相信各位对 React-Native 有所熟悉了吧,从现在开始我们要慢慢往实际的方向走,这边就先从网络请求这部分开始,在正式开发中,网络请求一般都单独作为一部分,我们在需要使用的地方只需要 ...

  5. HttpClient 常用方法封装

    简介 在平时写代码中,经常需要对接口进行访问,对于 http 协议 rest 风格的接口请求,大多使用 HttpClient 工具进行编写,想着方便就寻思着把一些常用的方法进行封装,便于平时快速的使用 ...

  6. flutter dio网络请求封装实现

    flutter dio网络请求封装实现 文章友情链接:   https://juejin.im/post/6844904098643312648 在Flutter项目中使用网络请求的方式大致可分为两种 ...

  7. 十. Axios网络请求封装

    1. 网络模块的选择 Vue中发送网络请求有非常多的方式,那么在开发中如何选择呢? 选择一:传统的Ajax是基于XMLHttpRequest(XHR) 为什么不用它呢?非常好解释配置和调用方式等非常混 ...

  8. iOS开发——post异步网络请求封装

    IOS中有许多网络请求的函数,同步的,异步的,通过delegate异步回调的. 在做一个项目的时候,上网看了很多别人的例子,发现都没有一个简单的,方便的异步请求的封装例子.我这里要给出的封装代码,是异 ...

  9. iOS开发-网络-合理封装请求接口

    概述 如今大多App都会与网络打交道,作为开发者,合理的对网络后台请求接口进行封装十分重要.本文要介绍的就是一种常见的采用回调函数(方法)的网络接口封装,也算的是一种构架吧. 这个构架主要的idea是 ...

随机推荐

  1. thinkPHP 获得当前请求的全部常量信息

    tp框架提供了常量: http://网址/shop/index.php/分组/控制器/操作方法/名称1/值/名称2/值 __MODULE__: 路由地址分组信息 (/shop/index.php/分组 ...

  2. IDEA编辑器

    一.打开含有jsx语法的文件都会显示红线,提示export declarations are not supported bu current javascript version 解决办法: 二.I ...

  3. 性能测试学习第五天-----Jmeter测试脚本&基础元件使用

    JMeter简介:一个100%的纯Java桌面应用,由Apache组织的开放源代码项目,它是功能和性能测试的工具.具有高可扩展性.支持Web(HTTP/HTTPS).SOAP.FTP.JAVA等多种协 ...

  4. 写论文的第三天 自建zookeeper集群

    日志___2019.1.25 基于hadoop集群搭建zookeeper集群 Filezilla上传zookeeper压缩包到主节点 安装zookeeper到/usr/local目录 命令:tar – ...

  5. Mysql主从复制原理及搭建

    ## Mysql主从复制原理 主从复制是指一台服务器充当主数据库服务器,另一台或多台服务器充当从数据库服务器,主服务器中的数据自动复制到从服务器之中.对于多级复制,数据库服务器即可充当主机,也可充当从 ...

  6. Ubuntu16.04中用yolov3训练自己的数据集

    一.配置yolo v3 参考yolo v3官网https://pjreddie.com/darknet/yolo/ 下载darknet后进行编译: git clone https://github.c ...

  7. CentOS搭建php + nginx环境

    更新Centos的yum源 yum update 安装EPEL源和REMI源 yum install epel-release yum install http://rpms.remirepo.net ...

  8. P2746 [USACO5.3]校园网Network of Schools tarjan 缩点

    题意 给出一个有向图,A任务:求最少需要从几个点送入信息,使得信息可以通过有向图走遍每一个点B任务:求最少需要加入几条边,使得有向图是一个强联通分量 思路 任务A,比较好想,可以通过tarjan缩点, ...

  9. codeforces1076 A.B.C.D.E

    1076A 1076B 1076C 1076D 1076D A. Minimizing the String  You are given a string s consisting of n low ...

  10. BZOJ-2535 航空管制 toposort

    题目传送门 题解: 如果正着连边,可以发现最困难的点是ti不好处理. 所以我们连反边,然后将ti转换成前面有n-ti+1架飞机起飞了作为限制条件. 对于第一问,直接toposort 然后反着输出求出的 ...