终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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