Нет описания

ChatCommon.cs 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Security;
  8. using System.Security.Cryptography.X509Certificates;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. namespace CallCenter.WebChatServer
  13. {
  14. public class ChatCommon
  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 static string HttpPost(string url, string param = null)
  24. {
  25. HttpWebRequest request;
  26. //如果是发送HTTPS请求
  27. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  28. {
  29. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  30. request = WebRequest.Create(url) as HttpWebRequest;
  31. request.ProtocolVersion = HttpVersion.Version10;
  32. }
  33. else
  34. {
  35. request = WebRequest.Create(url) as HttpWebRequest;
  36. }
  37. request.Method = "POST";
  38. request.ContentType = "application/x-www-form-urlencoded";
  39. request.Accept = "*/*";
  40. request.Timeout = 15000;
  41. request.AllowAutoRedirect = false;
  42. StreamWriter requestStream = null;
  43. WebResponse response = null;
  44. string responseStr = null;
  45. try
  46. {
  47. requestStream = new StreamWriter(request.GetRequestStream());
  48. requestStream.Write(param);
  49. requestStream.Close();
  50. response = request.GetResponse();
  51. if (response != null)
  52. {
  53. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  54. responseStr = reader.ReadToEnd();
  55. reader.Close();
  56. }
  57. }
  58. catch (Exception)
  59. {
  60. throw;
  61. }
  62. finally
  63. {
  64. request = null;
  65. requestStream = null;
  66. response = null;
  67. }
  68. return responseStr;
  69. }
  70. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  71. {
  72. return true; //总是接受
  73. }
  74. public static string BuildRequest(string strUrl, Dictionary<string, string> dicPara, string fileName)
  75. {
  76. string contentType = "image/jpeg";
  77. //待请求参数数组
  78. FileStream Pic = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  79. byte[] PicByte = new byte[Pic.Length];
  80. Pic.Read(PicByte, 0, PicByte.Length);
  81. int lengthFile = PicByte.Length;
  82. //构造请求地址
  83. //设置HttpWebRequest基本信息
  84. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
  85. //设置请求方式:get、post
  86. request.Method = "POST";
  87. //设置boundaryValue
  88. string boundaryValue = DateTime.Now.Ticks.ToString("x");
  89. string boundary = "--" + boundaryValue;
  90. request.ContentType = "\r\nmultipart/form-data; boundary=" + boundaryValue;
  91. //设置KeepAlive
  92. request.KeepAlive = true;
  93. //设置请求数据,拼接成字符串
  94. StringBuilder sbHtml = new StringBuilder();
  95. foreach (KeyValuePair<string, string> key in dicPara)
  96. {
  97. sbHtml.Append(boundary + "\r\nContent-Disposition: form-data; name=\"" + key.Key + "\"\r\n\r\n" + key.Value + "\r\n");
  98. }
  99. sbHtml.Append(boundary + "\r\nContent-Disposition: form-data; name=\"pic\"; filename=\"");
  100. sbHtml.Append(fileName);
  101. sbHtml.Append("\"\r\nContent-Type: " + contentType + "\r\n\r\n");
  102. string postHeader = sbHtml.ToString();
  103. //将请求数据字符串类型根据编码格式转换成字节流
  104. Encoding code = Encoding.GetEncoding("UTF-8");
  105. byte[] postHeaderBytes = code.GetBytes(postHeader);
  106. byte[] boundayBytes = Encoding.ASCII.GetBytes("\r\n" + boundary + "--\r\n");
  107. //设置长度
  108. long length = postHeaderBytes.Length + lengthFile + boundayBytes.Length;
  109. request.ContentLength = length;
  110. //请求远程HTTP
  111. Stream requestStream = request.GetRequestStream();
  112. Stream myStream = null;
  113. try
  114. {
  115. //发送数据请求服务器
  116. requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
  117. requestStream.Write(PicByte, 0, lengthFile);
  118. requestStream.Write(boundayBytes, 0, boundayBytes.Length);
  119. HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();
  120. myStream = HttpWResp.GetResponseStream();
  121. }
  122. catch (WebException e)
  123. {
  124. //LogResult(e.Message);
  125. return "";
  126. }
  127. finally
  128. {
  129. if (requestStream != null)
  130. {
  131. requestStream.Close();
  132. }
  133. }
  134. //读取处理结果
  135. StreamReader reader = new StreamReader(myStream, code);
  136. StringBuilder responseData = new StringBuilder();
  137. String line;
  138. while ((line = reader.ReadLine()) != null)
  139. {
  140. responseData.Append(line);
  141. }
  142. myStream.Close();
  143. Pic.Close();
  144. return responseData.ToString();
  145. }
  146. #endregion
  147. #region Get
  148. /// <summary>
  149. /// HTTP GET方式请求数据.
  150. /// </summary>
  151. /// <param name="url">URL.</param>
  152. /// <returns></returns>
  153. public static string HttpGet(string url, Hashtable headht = null)
  154. {
  155. HttpWebRequest request;
  156. //如果是发送HTTPS请求
  157. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  158. {
  159. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  160. request = WebRequest.Create(url) as HttpWebRequest;
  161. request.ProtocolVersion = HttpVersion.Version10;
  162. }
  163. else
  164. {
  165. request = WebRequest.Create(url) as HttpWebRequest;
  166. }
  167. request.Method = "GET";
  168. //request.ContentType = "application/x-www-form-urlencoded";
  169. request.Accept = "*/*";
  170. request.Timeout = 15000;
  171. request.AllowAutoRedirect = false;
  172. WebResponse response = null;
  173. string responseStr = null;
  174. if (headht != null)
  175. {
  176. foreach (DictionaryEntry item in headht)
  177. {
  178. request.Headers.Add(item.Key.ToString(), item.Value.ToString());
  179. }
  180. }
  181. try
  182. {
  183. response = request.GetResponse();
  184. if (response != null)
  185. {
  186. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  187. responseStr = reader.ReadToEnd();
  188. reader.Close();
  189. }
  190. }
  191. catch (Exception)
  192. {
  193. throw;
  194. }
  195. return responseStr;
  196. }
  197. public static string HttpGet(string url, Encoding encodeing, Hashtable headht = null)
  198. {
  199. HttpWebRequest request;
  200. //如果是发送HTTPS请求
  201. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  202. {
  203. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  204. request = WebRequest.Create(url) as HttpWebRequest;
  205. request.ProtocolVersion = HttpVersion.Version10;
  206. }
  207. else
  208. {
  209. request = WebRequest.Create(url) as HttpWebRequest;
  210. }
  211. request.Method = "GET";
  212. //request.ContentType = "application/x-www-form-urlencoded";
  213. request.Accept = "*/*";
  214. request.Timeout = 15000;
  215. request.AllowAutoRedirect = false;
  216. WebResponse response = null;
  217. string responseStr = null;
  218. if (headht != null)
  219. {
  220. foreach (DictionaryEntry item in headht)
  221. {
  222. request.Headers.Add(item.Key.ToString(), item.Value.ToString());
  223. }
  224. }
  225. try
  226. {
  227. response = request.GetResponse();
  228. if (response != null)
  229. {
  230. StreamReader reader = new StreamReader(response.GetResponseStream(), encodeing);
  231. responseStr = reader.ReadToEnd();
  232. reader.Close();
  233. }
  234. }
  235. catch (Exception)
  236. {
  237. throw;
  238. }
  239. return responseStr;
  240. }
  241. #endregion
  242. }
  243. }