终端一体化运控平台
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.

APIHelper.cs 3.2 KiB

2 年之前
2 年之前
2 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. namespace BPASmartClient.Http
  12. {
  13. public class APIHelper
  14. {
  15. private volatile static APIHelper _Instance;
  16. public static APIHelper GetInstance => _Instance ?? (_Instance = new APIHelper());
  17. private APIHelper() { }
  18. public string PostData(string url, string data, string head)
  19. {
  20. byte[] b = Encoding.UTF8.GetBytes(data);//把字符串转换为二进制
  21. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  22. request.Proxy = null;
  23. request.ContentType = "application/json";
  24. request.Method = "POST"; //设置请求方法
  25. request.ContentLength = b.Length; //设置长度
  26. request.Headers["Authorize"] = head;
  27. Stream postStream = request.GetRequestStream(); //requst流
  28. postStream.Write(b, 0, b.Length); //写入POST数据,二进制类型的
  29. postStream.Close(); //关闭
  30. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获取response
  31. Stream stream = response.GetResponseStream(); // 得到response响应流
  32. StreamReader sr = new StreamReader(stream);
  33. string str = sr.ReadToEnd(); //读取流
  34. sr.Close();
  35. stream.Close();
  36. return str;
  37. }
  38. public string GetData(string url, string head)
  39. {
  40. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  41. request.Method = "GET";
  42. request.Accept = "text/html, application/xhtml+xml, */*";
  43. request.ContentType = "application/json";
  44. request.Headers["Authorize"] = head;
  45. byte[] buffer = Encoding.UTF8.GetBytes(head);
  46. request.ContentLength = buffer.Length;
  47. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  48. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  49. using (StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  50. {
  51. return myStreamReader.ReadToEnd();
  52. }
  53. }
  54. public string HttpRequest(string url, string head, object data, RequestType requestType)
  55. {
  56. if (requestType == RequestType.POST)
  57. {
  58. return PostData(url, JsonConvert.SerializeObject(data), head);
  59. }
  60. else
  61. {
  62. StringBuilder sb = new StringBuilder();
  63. sb.Append("?");
  64. foreach (System.Reflection.PropertyInfo p in data.GetType().GetProperties())
  65. {
  66. if (sb.ToString().Last() != '?')
  67. {
  68. sb.Append("&");
  69. }
  70. sb.Append(p.Name);
  71. sb.Append("=");
  72. sb.Append(p.GetValue(data));
  73. }
  74. return GetData(url + sb.ToString(), head);
  75. }
  76. }
  77. }
  78. public enum RequestType
  79. {
  80. POST,
  81. PUT,
  82. DELETE,
  83. GET
  84. }
  85. }