HttpMethods  (C#.Net)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Net;
  7. using log4net;
  8.  
  9. namespace FaceppSDK
  10. {
  11. #region 提交方式
  12. class HttpMethods : IHttpMethod
  13. {
  14. readonly ILog logger = log4net.LogManager.GetLogger(typeof(HttpMethods));
  15.  
  16. #region POST
  17. /// <summary>
  18. /// HTTP POST方式请求数据
  19. /// </summary>
  20. /// <param name="url">URL.</param>
  21. /// <param name="param">POST的数据</param>
  22. /// <returns></returns>
  23. public virtual string HttpPost(string url, string param)
  24. {
  25. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  26. request.Method = "POST";
  27. request.ContentType = "application/x-www-form-urlencoded";
  28. request.Accept = "*/*";
  29. request.Timeout = 150000;
  30. request.AllowAutoRedirect = false;
  31. StreamWriter requestStream = null;
  32. WebResponse response = null;
  33. string responseStr = null;
  34.  
  35. try
  36. {
  37. requestStream = new StreamWriter(request.GetRequestStream());
  38. requestStream.Write(param);
  39. requestStream.Close();
  40.  
  41. response = request.GetResponse();
  42. if (response != null)
  43. {
  44. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  45. responseStr = reader.ReadToEnd();
  46. reader.Close();
  47. }
  48. }
  49. catch (Exception)
  50. {
  51. logger.Error("api 访问错误");
  52. }
  53. finally
  54. {
  55. request = null;
  56. requestStream = null;
  57. response = null;
  58. }
  59.  
  60. return responseStr;
  61. }
  62. #endregion
  63.  
  64. #region Get
  65. /// <summary>
  66. /// HTTP GET方式请求数据.
  67. /// </summary>
  68. /// <param name="url">URL.</param>
  69. /// <returns></returns>
  70. public virtual string HttpGet(string url)
  71. {
  72. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  73. request.Method = "GET";
  74. //request.ContentType = "application/x-www-form-urlencoded";
  75. request.Accept = "*/*";
  76. request.Timeout = 150000;
  77. request.AllowAutoRedirect = false;
  78.  
  79. WebResponse response = null;
  80. string responseStr = null;
  81.  
  82. try
  83. {
  84. response = request.GetResponse();
  85.  
  86. if (response != null)
  87. {
  88. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  89. responseStr = reader.ReadToEnd();
  90. reader.Close();
  91. }
  92. }
  93. catch (Exception)
  94. {
  95. logger.Error("api 访问错误");
  96. }
  97. finally
  98. {
  99. request = null;
  100. response = null;
  101. }
  102.  
  103. return responseStr;
  104. }
  105. #endregion
  106.  
  107. #region Post With Pic
  108. private string HttpPost(string url, IDictionary<object, object> param, string filePath)
  109. {
  110. string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
  111. byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
  112.  
  113. HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
  114. wr.ContentType = "multipart/form-data; boundary=" + boundary;
  115. wr.Method = "POST";
  116. wr.KeepAlive = true;
  117. wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
  118.  
  119. Stream rs = wr.GetRequestStream();
  120. string responseStr = null;
  121.  
  122. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
  123. foreach (string key in param.Keys)
  124. {
  125. rs.Write(boundarybytes, 0, boundarybytes.Length);
  126. string formitem = string.Format(formdataTemplate, key, param[key]);
  127. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  128. rs.Write(formitembytes, 0, formitembytes.Length);
  129. }
  130. rs.Write(boundarybytes, 0, boundarybytes.Length);
  131.  
  132. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
  133. string header = string.Format(headerTemplate, "img", filePath, "text/plain");
  134. byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  135. rs.Write(headerbytes, 0, headerbytes.Length);
  136.  
  137. FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  138. byte[] buffer = new byte[4096];
  139. int bytesRead = 0;
  140. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  141. {
  142. rs.Write(buffer, 0, bytesRead);
  143. }
  144. fileStream.Close();
  145.  
  146. byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
  147. rs.Write(trailer, 0, trailer.Length);
  148. rs.Close();
  149.  
  150. WebResponse wresp = null;
  151. try
  152. {
  153. wresp = wr.GetResponse();
  154. Stream stream2 = wresp.GetResponseStream();
  155. StreamReader reader2 = new StreamReader(stream2);
  156. responseStr = reader2.ReadToEnd();
  157. logger.Debug(string.Format("File uploaded, server response is: {0}", responseStr));
  158. }
  159. catch (Exception ex)
  160. {
  161. logger.Error("Error uploading file", ex);
  162. if (wresp != null)
  163. {
  164. wresp.Close();
  165. wresp = null;
  166. }
  167. }
  168. finally
  169. {
  170. wr = null;
  171. }
  172. return responseStr;
  173. }
  174. #endregion
  175.  
  176. #region Post With Pic
  177. /// <summary>
  178. /// HTTP POST方式请求数据(带图片)
  179. /// </summary>
  180. /// <param name="url">URL</param>
  181. /// <param name="param">POST的数据</param>
  182. /// <param name="fileByte">图片</param>
  183. /// <returns></returns>
  184. public virtual string HttpPost(string url, IDictionary<object, object> param, byte[] fileByte)
  185. {
  186. string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
  187. byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
  188.  
  189. HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
  190. wr.ContentType = "multipart/form-data; boundary=" + boundary;
  191. wr.Method = "POST";
  192. wr.KeepAlive = true;
  193. wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
  194.  
  195. Stream rs = wr.GetRequestStream();
  196. string responseStr = null;
  197.  
  198. string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
  199. foreach (string key in param.Keys)
  200. {
  201. rs.Write(boundarybytes, 0, boundarybytes.Length);
  202. string formitem = string.Format(formdataTemplate, key, param[key]);
  203. byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
  204. rs.Write(formitembytes, 0, formitembytes.Length);
  205. }
  206. rs.Write(boundarybytes, 0, boundarybytes.Length);
  207.  
  208. string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
  209. string header = string.Format(headerTemplate, "img", fileByte, "text/plain");//image/jpeg
  210. byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  211. rs.Write(headerbytes, 0, headerbytes.Length);
  212.  
  213. rs.Write(fileByte, 0, fileByte.Length);
  214.  
  215. byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
  216. rs.Write(trailer, 0, trailer.Length);
  217. rs.Close();
  218.  
  219. WebResponse wresp = null;
  220. try
  221. {
  222. wresp = wr.GetResponse();
  223. Stream stream2 = wresp.GetResponseStream();
  224. StreamReader reader2 = new StreamReader(stream2);
  225. responseStr = reader2.ReadToEnd();
  226. logger.Error(string.Format("File uploaded, server response is: {0}", responseStr));
  227. }
  228. catch (Exception ex)
  229. {
  230. logger.Error("Error uploading file", ex);
  231. if (wresp != null)
  232. {
  233. wresp.Close();
  234. wresp = null;
  235. }
  236. }
  237. finally
  238. {
  239. wr = null;
  240. }
  241. return responseStr;
  242. }
  243. #endregion
  244. }
  245. #endregion
  246.  
  247. #region 枚举
  248. /// <summary>
  249. /// 请求方式
  250. /// </summary>
  251. public enum Method
  252. {
  253. GET,
  254. POST,
  255. DELETE
  256. }
  257.  
  258. /// <summary>
  259. /// 返回格式
  260. /// </summary>
  261. public enum Format
  262. {
  263. xml,
  264. json,
  265. }
  266. #endregion
  267. }

HttpMethods(C#.net)的更多相关文章

  1. nmap --script http-enum,http-headers,http-methods,http-php-version -p 80 目标域

    从http服务器上收集到更多地信息 nmap --script http-enum,http-headers,http-methods,http-php-version  -p 80 目标域

  2. ★Kali信息收集★8.Nmap :端口扫描

    ★Kali信息收集~ 0.Httrack 网站复制机 http://www.cnblogs.com/dunitian/p/5061954.html ★Kali信息收集~ 1.Google Hackin ...

  3. 解读ASP.NET 5 & MVC6系列(12):基于Lamda表达式的强类型Routing实现

    前面的深入理解Routing章节,我们讲到了在MVC中,除了使用默认的ASP.NET 5的路由注册方式,还可以使用基于Attribute的特性(Route和HttpXXX系列方法)来定义.本章,我们将 ...

  4. nmap脚本扫描使用总结

    nmap的脚本默认目录为:/usr/share/nmap/scripts/ Nmap提供的命令行参数如下 -sC: 等价于--script=default,使用默认类别的脚本进行扫描 可更换其他类别 ...

  5. Nmap扫描手册

    By:WHILE扫描类-sTTCP connect()扫描,完整的通话连接,容易被检测,会被记录日志.-sSTCP同步扫描,不完整的通话连接,很少有系统会记入日志.-sUUDP扫描-sAACK扫描用来 ...

  6. nmap的script参数列表

    在新的nmap版本中,添加了script功能的使用.在nmap的安装目录的share/nmap/scripts中,已经有将61个写好的脚本提供. 具体的用法可以参考:http://nmap.org/b ...

  7. Android--Retrofit+RxJava的简单封装(三)

    1,继续接着上一篇的讲讲,话说如果像上一篇这样的话,那么我们每一次请求一个结构都要创建一堆的Retrofit对象,而且代码都是相同的,我们可以试试封装一下 先创建一个HttpMethods类,将Ret ...

  8. jetty 最后版本类库树, 基本上大多数应用都够了

    d:\jetty-distribution-8.1.17.v20150415\lib\annotations\javax.annotation-1.1.0.v201108011116.jarjavax ...

  9. PUT 还是 POST ?

    http://www.oschina.net/translate/put-or-post http://my.oschina.net/u/1263964/blog/268932 这两个方法咋一看都可以 ...

随机推荐

  1. Python Flask Jinja2模板引擎

    模板 简介 模板是一个包含响应文本的文件,其中包含用占位变量表示的动态部分,其具体值只在请 求的上下文中才能知道. 渲染 使用真实值替换变量,再返回最终得到的响应字符串,这一过程 称为渲染.为了渲染模 ...

  2. Android 数据库框架总结(转)

    转自 http://blog.csdn.net/da_caoyuan/article/details/61414626 一:OrmLite 简述: 优点: 1.轻量级:2.使用简单,易上手:3.封装完 ...

  3. 织梦栏目判断 seotitle的小bug

    有的栏目有seotitle(中文字符),有的没有,页面显示需要把seotitle放在括号中,所以进行了以下代码: {dede:field name="seotitle" runph ...

  4. inception+archery SQL审核平台

    关闭防火墙和selinux 宿主机安装mysql,创建archery数据库,并给所有权限,允许远程连接到该数据库 grant all privileges on *.* to 'root'@'%' i ...

  5. use crunch compression

    Crunch is a lossy compression format on top of DXTR texture compression. Textures will be converted ...

  6. http://www.bugku.com:Bugku——SQL注入1(http://103.238.227.13:10087/)

    Bugku——SQL注入1(http://103.238.227.13:10087/) 过滤了几乎所有的关键字,尝试绕过无果之后发现,下面有个xss过滤代码.经搜索得该函数会去掉所有的html标签,所 ...

  7. javascript简单的滑动效果

    利用setInterval实现简单的滑动效果 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  8. echarts中国地图坐标弹框

    echarts链接:http://gallery.echartsjs.com/editor.html?c=xHkdOlpwWz 代码: var geoCoordMap = { '上海': [121.4 ...

  9. c#、.net、asp.net、asp 、ado.net、.net framework的区别

    c#:一种编程语言 .net:一种运行环境 asp.net:基于.netFramework框架下的一种开发技术(相对与asp而言,引入了服务器控件,前后台可分,编译型的编程框架) asp:也是.net ...

  10. Spring 监听

    Spring 中的事件监听的实现 这里我们不讨论事件监听的机制的原理,我们只讨论如何在项目中实现时间监听. spring的事件监听是基于观察者模式.设计开发中.如下类与接口是我们必须要使用的. App ...