当前位置:   article > 正文

C# HTTP封装请求(包含Post,Get)_c# post get 封装

c# post get 封装
  1. string url = "https://restapi.amap.com/v3/distance?origins=abc";
  2. string res = HttpGet(url);
  3. string url1="http://zxcvbnm/Shop/AddOrder";
  4. string json = JsonConvert.SerializeObject(new { type = "1", order_id = "2" });
  5. string result = HttpPost(url1, json, "application/json");
  6. /// <summary>
  7. /// Get请求
  8. /// </summary>
  9. /// <param name="url">请求地址</param>
  10. /// <param name="Timeout">超时时间默认两分钟</param>
  11. /// <returns></returns>
  12. public static string HttpGet(string url, int Timeout=120000)
  13. {
  14. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  15. request.Method = "GET";
  16. request.ContentType = "text/html;charset=UTF-8";
  17. request.UserAgent = null;
  18. request.Timeout = Timeout;
  19. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  20. Stream myResponseStream = response.GetResponseStream();
  21. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  22. string retString = myStreamReader.ReadToEnd();
  23. myStreamReader.Close();
  24. myResponseStream.Close();
  25. return retString;
  26. }
  27. /// <summary>
  28. /// Post请求
  29. /// </summary>
  30. /// <param name="url">请求地址</param>
  31. /// <param name="pm_str">请求参数</param>
  32. /// <param name="pm_ContentType">参数类型</param>
  33. /// <param name="headstr">请求头</param>
  34. /// <returns></returns>
  35. public static string HttpPost(string url, string pm_str, string pm_ContentType = "appication/json", string headstr = null)
  36. {
  37. StreamReader reader = null;
  38. Stream requestStream = null;
  39. HttpWebResponse Response = null;
  40. try
  41. {
  42. HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
  43. ServicePointManager.DefaultConnectionLimit = int.MaxValue;
  44. myReq.Method = "POST";
  45. if (pm_ContentType != null)
  46. {
  47. myReq.ContentType = pm_ContentType;
  48. }
  49. if (headstr != null)
  50. {
  51. myReq.Headers.Add("Authorization", "Basic " + headstr);
  52. }
  53. byte[] bz = System.Text.Encoding.UTF8.GetBytes(pm_str);
  54. myReq.Timeout = 1800000;
  55. myReq.ContentLength = bz.Length;
  56. requestStream = myReq.GetRequestStream();
  57. requestStream.Write(bz, 0, bz.Length);
  58. requestStream.Close();
  59. requestStream = null;
  60. Response = (HttpWebResponse)myReq.GetResponse();
  61. reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
  62. string retstr = reader.ReadToEnd().Trim();
  63. return retstr;
  64. }
  65. catch (Exception ex)
  66. {
  67. return "{\"code\":\"0\",\"msg\":\"" + ex.Message.ToString() + "\"}";
  68. }
  69. finally
  70. {
  71. if (reader != null)
  72. {
  73. reader.Close();
  74. reader = null;
  75. }
  76. if (requestStream != null)
  77. {
  78. requestStream.Close();
  79. requestStream = null;
  80. }
  81. if (Response != null)
  82. {
  83. Response.Close();
  84. }
  85. }
  86. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号