HttpClient.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Security.Policy;
  8. using System.Text;
  9. using Newtonsoft.Json;
  10. namespace Aitex.Core.MES
  11. {
  12. internal class HttpClient
  13. {
  14. /// <summary>
  15. /// 调用mes基础方法
  16. /// </summary>
  17. /// <param name="url"></param>
  18. /// <param name="para"></param>
  19. /// <returns></returns>
  20. public static string HttpPost(string url, IDictionary<string, string> para)
  21. {
  22. try
  23. {
  24. Encoding encoding = Encoding.UTF8;
  25. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  26. request.Method = "POST";
  27. request.Accept = "text/html, application/xhtml+xml, */*";
  28. request.ContentType = "application/x-www-form-urlencoded ";//根据服务端进行 切换
  29. request.Timeout = 1000;
  30. StringBuilder buffer = new StringBuilder();
  31. int i = 0;
  32. foreach (string key in para.Keys)
  33. {
  34. if (i > 0)
  35. {
  36. buffer.AppendFormat("&{0}={1}", key, para[key]);
  37. }
  38. else
  39. {
  40. buffer.AppendFormat("{0}={1}", key, para[key]);
  41. }
  42. i++;
  43. }
  44. byte[] buffer2 = encoding.GetBytes(buffer.ToString());
  45. request.ContentLength = buffer2.Length;
  46. request.GetRequestStream().Write(buffer2, 0, buffer2.Length);
  47. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  48. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  49. {
  50. return reader.ReadToEnd();
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. return ex.Message;
  56. }
  57. }
  58. public static string PostResponse(string url, string postData, out string statusCode)
  59. {
  60. string result = string.Empty;
  61. //设置Http的正文
  62. HttpContent httpContent = new StringContent(postData);
  63. //设置Http的内容标头
  64. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
  65. //设置Http的内容标头的字符
  66. httpContent.Headers.ContentType.CharSet = "utf-8";
  67. using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())
  68. {
  69. //异步Post
  70. HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
  71. //输出Http响应状态码
  72. statusCode = response.StatusCode.ToString();
  73. //确保Http响应成功
  74. if (response.IsSuccessStatusCode)
  75. {
  76. //异步读取json
  77. result = response.Content.ReadAsStringAsync().Result;
  78. }
  79. }
  80. return result;
  81. }
  82. public static string HttpGet(string url)
  83. {
  84. string strRet = string.Empty;
  85. try
  86. {
  87. //url = "http://58.220.255.234:1005/api/mes/GetRecipeBySingleLot?strQuery=123";
  88. //把URL初始化为新的WebRequest
  89. HttpWebRequest Request = WebRequest.Create(url) as HttpWebRequest;
  90. Request.Method = "GET";
  91. //超时时间是2秒
  92. Request.Timeout = 2000;
  93. //初始化HTTP响应
  94. HttpWebResponse Response = Request.GetResponse() as HttpWebResponse;
  95. //获取HTTP响应流
  96. Stream resStream = Response.GetResponseStream();
  97. //读取数据流
  98. using (StreamReader reader = new StreamReader(resStream))
  99. {
  100. strRet= reader.ReadToEnd();
  101. return strRet;
  102. }
  103. ////把数据流放到Char数组中
  104. //Char[] read = new Char[256];
  105. //int count = readStream.Read(read, 0, 256);
  106. ////开始读
  107. //while (count > 0)
  108. //{
  109. // string str = new string(read, 0, count);
  110. // //读出
  111. // strRet += str;
  112. // count = readStream.Read(read, 0, 256);
  113. //}
  114. //readStream.Close();
  115. }
  116. catch (Exception ex)
  117. {
  118. strRet = ex.Message;
  119. }
  120. return strRet;
  121. }
  122. }
  123. }