HttpHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Ropin.Core.Common
  10. {
  11. public class HttpHelper
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. /// <param name="uri"></param>
  17. /// <param name="username"></param>
  18. /// <param name="password"></param>
  19. /// <returns></returns>
  20. public static string Get(string uri, string username, string password)
  21. {
  22. string result = string.Empty;
  23. WebClient client = new WebClient();
  24. if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
  25. {
  26. client.Credentials = GetCredentialCache(uri, username, password);
  27. client.Headers.Add("Authorization", GetAuthorization(username, password));
  28. }
  29. return client.DownloadString(uri);
  30. }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. /// <param name="uri"></param>
  35. /// <param name="paramStr"></param>
  36. /// <param name="username"></param>
  37. /// <param name="password"></param>
  38. /// <returns></returns>
  39. public static string Post(string uri, string paramStr, string username, string password)
  40. {
  41. string result = string.Empty;
  42. WebClient client = new WebClient();
  43. // 采取POST方式必须加的Header
  44. client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  45. byte[] postData = Encoding.UTF8.GetBytes(paramStr);
  46. if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
  47. {
  48. client.Credentials = GetCredentialCache(uri, username, password);
  49. client.Headers.Add("Authorization", GetAuthorization(username, password));
  50. }
  51. byte[] responseData = client.UploadData(uri, "POST", postData); // 得到返回字符流
  52. return Encoding.UTF8.GetString(responseData);// 解码
  53. }
  54. //public static string Post2(string uri, string paramStr, string username, string password)
  55. //{
  56. // HttpClient httpClient = httpClientFactory.CreateClient();
  57. // var msgData = new TpushMsgModel
  58. // {
  59. // C_DevStoreCode = devstore.C_ID,
  60. // C_MsgTypeCode = "MSG_TYPE_012",
  61. // Msg = devstore.C_Name + "的" + (string)ala["name"] + "设备点报警" + " 设备点值:" + (string)ala["value"],
  62. // Subject = "设备点报警",
  63. // DevNumber = devstore.C_NumberCode,
  64. // DevName = devstore.C_Name,
  65. // CreateOn = DateTime.Now.ToString(),
  66. // UserName = "设备",
  67. // };
  68. // var httpRequestMessage = new HttpRequestMessage
  69. // {
  70. // Method = HttpMethod.Post,
  71. // RequestUri = new Uri(uri),
  72. // Content = new StringContent(JsonConvert.SerializeObject(msgData), Encoding.UTF8, "application/json")
  73. // };
  74. // var response = await httpClient.SendAsync(httpRequestMessage);
  75. // string responseResult = await response.Content.ReadAsStringAsync();
  76. // if (response.StatusCode != HttpStatusCode.OK)
  77. // {
  78. // }
  79. // else
  80. // {
  81. // //todo
  82. // }
  83. //}
  84. private static CredentialCache GetCredentialCache(string uri, string username, string password)
  85. {
  86. string authorization = string.Format("{0}:{1}", username, password);
  87. CredentialCache credCache = new CredentialCache();
  88. credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
  89. return credCache;
  90. }
  91. private static string GetAuthorization(string username, string password)
  92. {
  93. string authorization = string.Format("{}:{1}", username, password);
  94. return "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization));
  95. }
  96. }
  97. }