地铁二期项目正式开始

HttpHelper.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Security.Cryptography;
  8. using System.Net.Http;
  9. using System.Net.Http.Headers;
  10. using System.Configuration;
  11. namespace YTSoft.BaseCallCenter.MVCWeb.Commons
  12. {
  13. public class HttpHelper
  14. {
  15. //获取配置文件中api借口IP信息
  16. /// <summary>
  17. /// 通过get获取数据
  18. /// </summary>
  19. /// <param name="controllerName">/控制器名称/Action名称</param>
  20. /// <param name="filters">查询条件字符串</param>
  21. /// <returns></returns>
  22. public static string HttpGet(string controllerActionName,string filters)
  23. {
  24. //访问路径
  25. string httpUrl = controllerActionName + (string.IsNullOrEmpty(filters.Trim()) ? "" : "?") + filters;
  26. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpUrl);
  27. request.KeepAlive = false;
  28. request.Method = "GET";
  29. request.ContentType = "text/html;charset=UTF-8";
  30. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  31. Stream myResponseStream = response.GetResponseStream();
  32. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  33. string retString = myStreamReader.ReadToEnd();
  34. myStreamReader.Close();
  35. myResponseStream.Close();
  36. return retString;
  37. }
  38. //获取配置文件中api借口IP信息
  39. /// <summary>
  40. /// 通过get获取数据
  41. /// </summary>
  42. /// <param name="controllerName">/控制器名称/Action名称</param>
  43. /// <param name="filters">查询条件字符串</param>
  44. /// <returns></returns>
  45. public static string HttpSMSSend(string httpUrl)
  46. {
  47. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(httpUrl);
  48. request.KeepAlive = false;
  49. request.Method = "GET";
  50. request.ContentType = "text/html;charset=UTF-8";
  51. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  52. Stream myResponseStream = response.GetResponseStream();
  53. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  54. string retString = myStreamReader.ReadToEnd();
  55. myStreamReader.Close();
  56. myResponseStream.Close();
  57. return retString;
  58. }
  59. public static string HttpPost(string controllerActionName, object json)
  60. {
  61. string result = string.Empty;
  62. try
  63. {
  64. HttpClient client = new HttpClient();
  65. string httpUrl = controllerActionName;
  66. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  67. var response = client.PostAsJsonAsync(httpUrl, json).Result;
  68. if (response.IsSuccessStatusCode)
  69. {
  70. result = response.Content.ReadAsStringAsync().Result;
  71. //response.Content.ReadAsAsync<Object>().Result.ToString();
  72. }
  73. else
  74. {
  75. result = response.ReasonPhrase;
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. throw new Exception(ex.Message);
  81. }
  82. return result;
  83. }
  84. public static string HttpGet(string url)
  85. {
  86. string result = string.Empty;
  87. try
  88. {
  89. HttpClient client = new HttpClient();
  90. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  91. var task = client.GetAsync(new Uri(url)).Result;
  92. if (task.IsSuccessStatusCode)
  93. {
  94. result = task.Content.ReadAsAsync<Object>().Result.ToString();
  95. }
  96. else
  97. {
  98. result = task.ReasonPhrase;
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. throw new Exception(ex.Message);
  104. }
  105. return result;
  106. }
  107. public static byte[] File2HttpBytes(string imagepath)
  108. {
  109. WebRequest webRequest = WebRequest.Create(imagepath);
  110. WebResponse webResponse = webRequest.GetResponse();
  111. Stream stream = webResponse.GetResponseStream();
  112. MemoryStream mem = new MemoryStream();
  113. BufferedStream bfs = new BufferedStream(stream);
  114. int len = 0;
  115. byte[] buffer = new byte[4096];
  116. do
  117. {
  118. len = bfs.Read(buffer, 0, buffer.Length);
  119. if (len > 0)
  120. mem.Write(buffer, 0, len);
  121. }
  122. while (len > 0);
  123. bfs.Close();
  124. byte[] picbytes = mem.ToArray();
  125. mem.Close();
  126. return picbytes;
  127. }
  128. /// <summary>
  129. /// 将文件转换为byte数组
  130. /// </summary>
  131. /// <param name="path">文件地址</param>
  132. /// <returns>转换后的byte数组</returns>
  133. public static byte[] File2Bytes(string path)
  134. {
  135. if (!System.IO.File.Exists(path))
  136. {
  137. return new byte[0];
  138. }
  139. FileInfo fi = new FileInfo(path);
  140. byte[] buff = new byte[fi.Length];
  141. FileStream fs = fi.OpenRead();
  142. fs.Read(buff, 0, Convert.ToInt32(fs.Length));
  143. fs.Close();
  144. return buff;
  145. }
  146. /// <summary>
  147. /// MD5函数
  148. /// </summary>
  149. /// <param name="str">原始字符串</param>
  150. /// <returns>MD5结果</returns>
  151. public static string DZ_MD5(string str)
  152. {
  153. byte[] b = Encoding.Default.GetBytes(str);
  154. b = new MD5CryptoServiceProvider().ComputeHash(b);
  155. string ret = "";
  156. for (int i = 0; i < b.Length; i++)
  157. ret += b[i].ToString("X").PadLeft(2, '0');
  158. return ret;
  159. }
  160. }
  161. }