No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

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