基础服务api
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.
 
 

73 lines
2.7 KiB

  1. using System.Net;
  2. using System.Text;
  3. namespace BPA.SAAS.Manage.Comm.Util
  4. {
  5. public static class HttpHelper
  6. {
  7. private static readonly HttpClient client = new HttpClient();
  8. /// <summary>
  9. /// Get 请求
  10. /// </summary>
  11. /// <param name="url"></param>
  12. /// <param name="postDataStr"></param>
  13. /// <param name="headers"></param>
  14. /// <param name="platform"></param>
  15. /// <param name="contentType"></param>
  16. /// <returns></returns>
  17. public static string HttpGet(string url, string postDataStr, Dictionary<string, string> headers, string contentType = "")
  18. {
  19. var result = string.Empty;
  20. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + postDataStr);
  21. request.ServicePoint.Expect100Continue = false; //加了这一行代码 就OK了
  22. request.Method = "GET";
  23. request.ContentType = contentType;
  24. foreach (KeyValuePair<string, string> kv in headers)
  25. {
  26. request.Headers.Add(kv.Key, kv.Value);
  27. }
  28. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  29. Stream myResponseStream = response.GetResponseStream();
  30. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  31. result = myStreamReader.ReadToEnd();
  32. myStreamReader.Close();
  33. myResponseStream.Close();
  34. return result;
  35. }
  36. public static string PostData(string url, string postData, Encoding encoding, string ContentType, Dictionary<string, string> headers)
  37. {
  38. HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
  39. myReq.Method = "POST";
  40. myReq.Timeout = 600 * 60;
  41. byte[] byteArray = encoding.GetBytes(postData);
  42. myReq.ContentType = ContentType;
  43. //对键值对进行遍历
  44. foreach (KeyValuePair<string, string> kv in headers)
  45. {
  46. myReq.Headers.Add(kv.Key, kv.Value);
  47. }
  48. Stream dataStream = myReq.GetRequestStream();
  49. dataStream.Write(byteArray, 0, byteArray.Length);
  50. dataStream.Close();
  51. HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
  52. dataStream = HttpWResp.GetResponseStream();
  53. string result = "";
  54. using (StreamReader myStreamReader = new StreamReader(dataStream, encoding))
  55. {
  56. result = myStreamReader.ReadToEnd();
  57. myStreamReader.Close();
  58. }
  59. dataStream.Close();
  60. HttpWResp.Close();
  61. return result;
  62. }
  63. }
  64. }