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.
 
 

139 lines
5.1 KiB

  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10. using Newtonsoft.Json;
  11. using HBLConsole.Service;
  12. namespace HBLConsole.Service
  13. {
  14. public class APIHelper
  15. {
  16. private volatile static APIHelper _Instance;
  17. public static APIHelper GetInstance => _Instance ?? (_Instance = new APIHelper());
  18. private APIHelper() { }
  19. /// <summary>
  20. /// 获取物料信息
  21. /// </summary>
  22. /// <param name="ClientId"></param>
  23. /// <returns></returns>
  24. //public OrderMaterialDelivery GetBatchingInfo(int ClientId)
  25. //{
  26. // string result = string.Empty;
  27. // try
  28. // {
  29. // var jsondata = new { ClientId };
  30. // string header = $"[{InternetInfo.GetInstance.StockServer}/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
  31. // string url = $"{InternetInfo.GetInstance.ApiAddress}{InternetInfo.GetInstance.StockServer}/GetItemInfo";
  32. // result = HttpRequest(url, header, jsondata, RequestType.POST);
  33. // }
  34. // catch (Exception ex)
  35. // {
  36. // MessageLog.GetInstance.Show(ex.ToString());
  37. // }
  38. // return JsonConvert.DeserializeObject<OrderMaterialDelivery>(result);
  39. //}
  40. ///// <summary>
  41. ///// 更改订单状态
  42. ///// </summary>
  43. ///// <param name=""></param>
  44. ///// <returns></returns>
  45. //public OrderStatusChangeRsp OrderStatusChange(OrderStatusChange orderStatusChange)
  46. //{
  47. // string result = string.Empty;
  48. // try
  49. // {
  50. // string header = $"[{InternetInfo.GetInstance.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
  51. // string url = $"{InternetInfo.GetInstance.ApiAddress}{InternetInfo.GetInstance.OrderServer}/order/robotstatuschange";
  52. // result = HttpRequest(url, header, orderStatusChange, RequestType.POST);
  53. // }
  54. // catch (Exception ex)
  55. // {
  56. // MessageLog.GetInstance.Show(ex.ToString());
  57. // }
  58. // return JsonConvert.DeserializeObject<OrderStatusChangeRsp>(result);
  59. //}
  60. public string PostData(string url, string data, string head)
  61. {
  62. byte[] b = Encoding.UTF8.GetBytes(data);//把字符串转换为二进制
  63. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  64. request.Proxy = null;
  65. request.ContentType = "application/json";
  66. request.Method = "POST"; //设置请求方法
  67. request.ContentLength = b.Length; //设置长度
  68. request.Headers["Authorize"] = head;
  69. Stream postStream = request.GetRequestStream(); //requst流
  70. postStream.Write(b, 0, b.Length); //写入POST数据,二进制类型的
  71. postStream.Close(); //关闭
  72. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获取response
  73. Stream stream = response.GetResponseStream(); // 得到response响应流
  74. StreamReader sr = new StreamReader(stream);
  75. string str = sr.ReadToEnd(); //读取流
  76. sr.Close();
  77. stream.Close();
  78. return str;
  79. }
  80. public string GetData(string url, string head)
  81. {
  82. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  83. request.Method = "GET";
  84. request.Accept = "text/html, application/xhtml+xml, */*";
  85. request.ContentType = "application/json";
  86. request.Headers["Authorize"] = head;
  87. byte[] buffer = Encoding.UTF8.GetBytes(head);
  88. request.ContentLength = buffer.Length;
  89. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  90. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  91. using (StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  92. {
  93. return myStreamReader.ReadToEnd();
  94. }
  95. }
  96. public string HttpRequest(string url, string head, object data, RequestType requestType)
  97. {
  98. if (requestType == RequestType.POST)
  99. {
  100. return PostData(url, JsonConvert.SerializeObject(data), head);
  101. }
  102. else
  103. {
  104. StringBuilder sb = new StringBuilder();
  105. sb.Append("?");
  106. foreach (System.Reflection.PropertyInfo p in data.GetType().GetProperties())
  107. {
  108. if (sb.ToString().Last() != '?')
  109. {
  110. sb.Append("&");
  111. }
  112. sb.Append(p.Name);
  113. sb.Append("=");
  114. sb.Append(p.GetValue(data));
  115. }
  116. return GetData(url + sb.ToString(), head);
  117. }
  118. }
  119. }
  120. public enum RequestType
  121. {
  122. POST,
  123. PUT,
  124. DELETE,
  125. GET
  126. }
  127. }