圣珀
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

473 lines
20 KiB

2 years ago
  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.Threading.Tasks;
  8. namespace ICS.WCF.Base
  9. {
  10. public class PostData
  11. {
  12. /// <summary>
  13. ///general a get http request
  14. /// </summary>
  15. /// <author>Johnny</author>
  16. /// <date>2013/11/20, 09:21:33</date>
  17. public static string Get(string url, Dictionary<string, string> parameters)
  18. {
  19. return Get(url, DictionaryToString(parameters));
  20. }
  21. /// <summary>
  22. ///general a get http request
  23. /// </summary>
  24. /// <author>Johnny</author>
  25. /// <date>2013/11/20, 09:21:33</date>
  26. public static string Get(string url, string parameters)
  27. {
  28. if (parameters != null && parameters != "")
  29. {
  30. if (url.Contains("?"))
  31. {
  32. url += "&" + parameters;
  33. }
  34. else
  35. {
  36. url += "?" + parameters;
  37. }
  38. }
  39. WebRequest request = WebRequest.Create(url);
  40. WebResponse response = request.GetResponse();
  41. Stream stream = response.GetResponseStream();
  42. StreamReader reader = new StreamReader(stream, Encoding.UTF8);
  43. string content = reader.ReadToEnd();
  44. reader.Close();
  45. stream.Close();
  46. return content;
  47. }
  48. /// <summary>
  49. ///general a http request with add header type
  50. /// </summary>
  51. /// <author>Johnny</author>
  52. /// <date>2013/11/26, 17:12:32</date>
  53. public static string HeaderRequest(string method, string url, string headerQuery)
  54. {
  55. try
  56. {
  57. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  58. request.Method = method;
  59. request.Headers.Add(headerQuery);
  60. request.Credentials = CredentialCache.DefaultCredentials;
  61. WebResponse response = request.GetResponse();
  62. Stream stream = response.GetResponseStream();
  63. StreamReader reader = new StreamReader(stream, Encoding.UTF8);
  64. string content = reader.ReadToEnd();
  65. reader.Close();
  66. stream.Close();
  67. return content;
  68. }
  69. catch (Exception)
  70. {
  71. throw;
  72. }
  73. }
  74. /// <summary>
  75. ///general a post http request
  76. /// </summary>
  77. /// <author>Johnny</author>
  78. /// <date>2013/11/20, 09:21:33</date>
  79. public static string Post(string url, Dictionary<string, string> parameters)
  80. {
  81. return Post(url, DictionaryToString(parameters));
  82. }
  83. /// <summary>
  84. ///general a post http request
  85. /// </summary>
  86. /// <author>Johnny</author>
  87. /// <date>2013/11/20, 09:21:33</date>
  88. public static string Post(string url, string parameters)
  89. {
  90. byte[] postData = System.Text.Encoding.ASCII.GetBytes(parameters);
  91. System.Net.ServicePointManager.Expect100Continue = false;
  92. WebRequest request = WebRequest.Create(url);
  93. request.Method = "POST";
  94. request.ContentType = "application/x-www-form-urlencoded";
  95. request.ContentLength = postData.Length;
  96. Stream requestStream = request.GetRequestStream();
  97. requestStream.Write(postData, 0, postData.Length);
  98. requestStream.Close();
  99. WebResponse response = request.GetResponse();
  100. Stream stream = response.GetResponseStream();
  101. StreamReader reader = new StreamReader(stream, Encoding.UTF8);
  102. string content = reader.ReadToEnd();
  103. reader.Close();
  104. stream.Close();
  105. return content;
  106. }
  107. public static string APT(string URL, string JsonData, string token)
  108. {
  109. try
  110. {
  111. //调用接口获取返回信息
  112. //string ContentType = "application/json;charset=UTF-8;";//application/json
  113. string ContentType = "application/json";
  114. WebRequest request = WebRequest.Create(URL);
  115. request.Method = "POST";
  116. request.Headers.Add("token", token);
  117. byte[] bytes = System.Text.Encoding.UTF8.GetBytes(JsonData);
  118. request.ContentType = ContentType;
  119. request.ContentLength = bytes.Length;
  120. using (Stream postStream = request.GetRequestStream())
  121. {
  122. postStream.Write(bytes, 0, bytes.Length);
  123. }
  124. request.Credentials = CredentialCache.DefaultCredentials;
  125. string str = string.Empty;
  126. WebResponse response = null;
  127. try
  128. {
  129. response = request.GetResponse();
  130. }
  131. catch (WebException ex)
  132. {
  133. if (ex.Status == WebExceptionStatus.ProtocolError)
  134. response = (WebResponse)ex.Response;
  135. }
  136. if (response != null)
  137. {
  138. using (StreamReader st = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
  139. {
  140. str = System.Web.HttpUtility.UrlDecode(st.ReadToEnd());
  141. }
  142. }
  143. //将返回的string类型转换成model
  144. //BaseModel obj = (BaseModel)Newtonsoft.Json.JsonConvert.DeserializeObject(str, typeof(BaseModel));
  145. return str;
  146. }
  147. catch (Exception ex)
  148. {
  149. throw new Exception("调用MES接口错误:" + ex.Message);
  150. }
  151. }
  152. public static string Post(string url, string parameters, string token)
  153. {
  154. byte[] postData = System.Text.Encoding.ASCII.GetBytes(parameters);
  155. System.Net.ServicePointManager.Expect100Continue = false;
  156. WebRequest request = WebRequest.Create(url);
  157. request.Method = "POST";
  158. request.ContentType = "application/x-www-form-urlencoded";
  159. request.ContentLength = postData.Length;
  160. //request.Headers.Add("Authorization", token);
  161. request.Headers.Add("token", token);
  162. Stream requestStream = request.GetRequestStream();
  163. requestStream.Write(postData, 0, postData.Length);
  164. requestStream.Close();
  165. WebResponse response = request.GetResponse();
  166. Stream stream = response.GetResponseStream();
  167. StreamReader reader = new StreamReader(stream, Encoding.UTF8);
  168. string content = reader.ReadToEnd();
  169. reader.Close();
  170. stream.Close();
  171. return content;
  172. }
  173. /// <summary>
  174. ///general the result string to dictionary
  175. /// </summary>
  176. /// <author>Johnny</author>
  177. /// <date>2013/11/20, 09:23:25</date>
  178. public static string DictionaryToString(Dictionary<string, string> parameters)
  179. {
  180. string queryParameter = "";
  181. foreach (string key in parameters.Keys)
  182. {
  183. if (queryParameter != "") queryParameter = "&";
  184. queryParameter += key + "=" + parameters[key];
  185. }
  186. return queryParameter;
  187. }
  188. /// <summary>
  189. ///general the result dictionary to string
  190. /// </summary>
  191. /// <author>Johnny</author>
  192. /// <date>2013/11/20, 09:23:25</date>
  193. public static Dictionary<string, string> StringToDictionary(string queryParameter)
  194. {
  195. Dictionary<string, string> parameters = new Dictionary<string, string>();
  196. foreach (string keyvalue in queryParameter.Split(new char[] { '&' }))
  197. {
  198. string[] values = keyvalue.Split(new char[] { '=' });
  199. parameters.Add(values[0], values[1]);
  200. }
  201. return parameters;
  202. }
  203. /// <summary>
  204. ///general a post http request
  205. /// </summary>
  206. /// <author>Johnny</author>
  207. /// <date>2013/11/20, 09:21:33</date>
  208. public static string PostByToken(string url, string parameters, string token)
  209. {
  210. //if (url.ToLower().StartsWith("https"))
  211. //{
  212. // ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
  213. // | SecurityProtocolType.Tls
  214. // ;
  215. //}
  216. ServicePointManager.Expect100Continue = true;
  217. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  218. ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
  219. byte[] postData = System.Text.Encoding.ASCII.GetBytes(parameters);
  220. //System.Net.ServicePointManager.Expect100Continue = false;
  221. WebRequest request = WebRequest.Create(url);
  222. request.Method = "POST";
  223. request.ContentType = "application/x-www-form-urlencoded";
  224. request.ContentLength = postData.Length;
  225. request.Headers.Add("Authorization", token);
  226. Stream requestStream = request.GetRequestStream();
  227. requestStream.Write(postData, 0, postData.Length);
  228. requestStream.Close();
  229. WebResponse response = request.GetResponse();
  230. Stream stream = response.GetResponseStream();
  231. StreamReader reader = new StreamReader(stream, Encoding.UTF8);
  232. string content = reader.ReadToEnd();
  233. reader.Close();
  234. stream.Close();
  235. return content;
  236. }
  237. /// <summary>
  238. ///general a post http request
  239. /// </summary>
  240. /// <author>Johnny</author>
  241. /// <date>2013/11/20, 09:21:33</date>
  242. public static string PostByToken(string url, Dictionary<string, string> parameters, string token)
  243. {
  244. return PostByToken(url, DictionaryToString(parameters), token);
  245. }
  246. public static string HttpPost(string url, string body, string token, string MethodType)
  247. {
  248. try
  249. {
  250. Encoding encoding = Encoding.UTF8;
  251. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  252. request.Method = MethodType;
  253. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  254. request.ContentType = "application/json; charset=utf-8";
  255. request.Headers.Add("Authorization", token);
  256. byte[] buffer = encoding.GetBytes(body);
  257. request.ContentLength = buffer.Length;
  258. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  259. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  260. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  261. {
  262. return reader.ReadToEnd();
  263. }
  264. }
  265. catch (WebException ex)
  266. {
  267. var res = (HttpWebResponse)ex.Response;
  268. StringBuilder sb = new StringBuilder();
  269. StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
  270. sb.Append(sr.ReadToEnd());
  271. //string ssb = sb.ToString();
  272. throw new Exception(sb.ToString());
  273. }
  274. }
  275. public static string PostByTokenGet(string url, Dictionary<string, string> parameters, string token)
  276. {
  277. //return PostByTokenGet(url, DictionaryToString(parameters), token);
  278. return GetInfo("", url, token);
  279. }
  280. public static string PostByTokenGet(string url, string parameters, string token)
  281. {
  282. if (url.ToLower().StartsWith("https"))
  283. {
  284. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
  285. | SecurityProtocolType.Tls
  286. ;
  287. }
  288. byte[] postData = System.Text.Encoding.ASCII.GetBytes(parameters);
  289. System.Net.ServicePointManager.Expect100Continue = false;
  290. WebRequest request = WebRequest.Create(url);
  291. request.Method = "GET";
  292. request.ContentType = "application/x-www-form-urlencoded";
  293. request.ContentLength = postData.Length;
  294. request.Headers.Add("Authorization", token);
  295. Stream requestStream = request.GetRequestStream();
  296. requestStream.Write(postData, 0, postData.Length);
  297. requestStream.Close();
  298. WebResponse response = request.GetResponse();
  299. Stream stream = response.GetResponseStream();
  300. StreamReader reader = new StreamReader(stream, Encoding.UTF8);
  301. string content = reader.ReadToEnd();
  302. reader.Close();
  303. stream.Close();
  304. return content;
  305. }
  306. public static string GetInfo(string postData, string Url)
  307. {
  308. try
  309. {
  310. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  311. HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(Url);
  312. objWebRequest.Method = "Get";
  313. objWebRequest.ContentType = "application/json;charset=UTF-8";
  314. objWebRequest.ContentLength = byteArray.Length;
  315. //objWebRequest.Headers.Add("Authorization", token);
  316. HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();
  317. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  318. string textResponse = sr.ReadToEnd(); // 返回的数据
  319. return textResponse;
  320. }
  321. catch (Exception ex)
  322. {
  323. throw new Exception(ex.Message);
  324. }
  325. }
  326. public static string GetInfo(string postData, string Url, string token)
  327. {
  328. try
  329. {
  330. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  331. HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(Url);
  332. objWebRequest.Method = "Get";
  333. objWebRequest.ContentType = "application/json;charset=UTF-8";
  334. objWebRequest.ContentLength = byteArray.Length;
  335. objWebRequest.Headers.Add("Authorization", token);
  336. HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();
  337. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  338. string textResponse = sr.ReadToEnd(); // 返回的数据
  339. return textResponse;
  340. }
  341. catch (Exception ex)
  342. {
  343. throw new Exception(ex.Message);
  344. }
  345. }
  346. public static string PostInfo(string postData, string Url, string token)
  347. {
  348. try
  349. {
  350. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  351. HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(Url);
  352. objWebRequest.Method = "Post";
  353. objWebRequest.ContentType = "application/json;charset=UTF-8";
  354. objWebRequest.ContentLength = byteArray.Length;
  355. //objWebRequest.Headers.Add("Authorization", token);
  356. objWebRequest.Headers.Add("token", token);
  357. HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();
  358. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  359. string textResponse = sr.ReadToEnd(); // 返回的数据
  360. return textResponse;
  361. }
  362. catch (Exception ex)
  363. {
  364. throw new Exception(ex.Message);
  365. }
  366. }
  367. public static string HttpPatch(string url, string body, string token)
  368. {
  369. try
  370. {
  371. Encoding encoding = Encoding.UTF8;
  372. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  373. request.Method = "PATCH";
  374. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  375. request.ContentType = "application/json; charset=utf-8";
  376. request.Headers.Add("Authorization", token);
  377. byte[] buffer = encoding.GetBytes(body);
  378. request.ContentLength = buffer.Length;
  379. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  380. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  381. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  382. {
  383. return reader.ReadToEnd();
  384. }
  385. }
  386. catch (WebException ex)
  387. {
  388. var res = (HttpWebResponse)ex.Response;
  389. StringBuilder sb = new StringBuilder();
  390. StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
  391. sb.Append(sr.ReadToEnd());
  392. //string ssb = sb.ToString();
  393. throw new Exception(sb.ToString());
  394. }
  395. }
  396. public static string HttpDelete(string url, string body, string token)
  397. {
  398. try
  399. {
  400. Encoding encoding = Encoding.UTF8;
  401. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  402. request.Method = "DELETE";
  403. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  404. request.ContentType = "application/json; charset=utf-8";
  405. request.Headers.Add("Authorization", token);
  406. byte[] buffer = encoding.GetBytes(body);
  407. request.ContentLength = buffer.Length;
  408. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  409. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  410. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  411. {
  412. return reader.ReadToEnd();
  413. }
  414. }
  415. catch (WebException ex)
  416. {
  417. var res = (HttpWebResponse)ex.Response;
  418. StringBuilder sb = new StringBuilder();
  419. StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
  420. sb.Append(sr.ReadToEnd());
  421. //string ssb = sb.ToString();
  422. throw new Exception(sb.ToString());
  423. }
  424. }
  425. }
  426. }