团餐订单
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.
 
 

101 lines
3.5 KiB

  1. using Furion;
  2. using Furion.FriendlyException;
  3. using Furion.Templates;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  6. using Microsoft.IdentityModel.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Net.Http;
  12. using System.Reflection.PortableExecutable;
  13. using System.Security.Policy;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace BPA.KitChen.GroupMealOrder.Core.Common
  17. {
  18. public static class HttpHelper
  19. {
  20. private static readonly HttpClient client = new HttpClient();
  21. /// <summary>
  22. /// Get 请求
  23. /// </summary>
  24. /// <param name="url"></param>
  25. /// <param name="postDataStr"></param>
  26. /// <param name="headers"></param>
  27. /// <param name="platform"></param>
  28. /// <param name="contentType"></param>
  29. /// <returns></returns>
  30. public static string HttpGet(string url, string postDataStr, Dictionary<string, string> headers, string contentType = "")
  31. {
  32. var result = string.Empty;
  33. try
  34. {
  35. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + postDataStr);
  36. request.ServicePoint.Expect100Continue = false; //加了这一行代码 就OK了
  37. request.Method = "GET";
  38. request.ContentType = contentType;
  39. foreach (KeyValuePair<string, string> kv in headers)
  40. {
  41. request.Headers.Add(kv.Key, kv.Value);
  42. }
  43. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  44. Stream myResponseStream = response.GetResponseStream();
  45. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  46. result = myStreamReader.ReadToEnd();
  47. myStreamReader.Close();
  48. myResponseStream.Close();
  49. }
  50. catch (Exception ex)
  51. {
  52. throw Oops.Oh( ex.Message);
  53. }
  54. return result;
  55. }
  56. public static string PostData(string url, string postData, Encoding encoding, string ContentType, Dictionary<string, string> headers)
  57. {
  58. try
  59. {
  60. HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
  61. myReq.Method = "POST";
  62. myReq.Timeout = 600 * 60;
  63. byte[] byteArray = encoding.GetBytes(postData);
  64. myReq.ContentType = ContentType;
  65. //对键值对进行遍历
  66. foreach (KeyValuePair<string, string> kv in headers)
  67. {
  68. myReq.Headers.Add(kv.Key, kv.Value);
  69. }
  70. Stream dataStream = myReq.GetRequestStream();
  71. dataStream.Write(byteArray, 0, byteArray.Length);
  72. dataStream.Close();
  73. HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
  74. dataStream = HttpWResp.GetResponseStream();
  75. string result = "";
  76. using (StreamReader myStreamReader = new StreamReader(dataStream, encoding))
  77. {
  78. result = myStreamReader.ReadToEnd();
  79. myStreamReader.Close();
  80. }
  81. dataStream.Close();
  82. HttpWResp.Close();
  83. return result;
  84. }
  85. catch (System.Exception ex)
  86. {
  87. throw ex;
  88. }
  89. }
  90. }
  91. }