WechatHelper.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using QWPlatform.SystemLibrary.LogManager;
  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. namespace PMS.BusinessModels.ModelExtend
  10. {
  11. public class HttpCrossDomain
  12. {
  13. /// <summary>
  14. /// 跨域访问
  15. /// </summary>
  16. /// <param name="url"></param>
  17. /// <param name="param"></param>
  18. /// <returns></returns>
  19. public static string Post(string url, string param, int time = 60000)
  20. {
  21. try
  22. {
  23. var address = new Uri(url);
  24. var request = WebRequest.Create(address) as HttpWebRequest;
  25. request.Method = "POST";
  26. request.ContentType = "application/json;charset=utf-8"; //"application/x-www-form-urlencoded";
  27. request.Timeout = time;
  28. var byteData = Encoding.UTF8.GetBytes(param == null ? "" : param);
  29. request.ContentLength = byteData.Length;
  30. using (var postStream = request.GetRequestStream())
  31. {
  32. postStream.Write(byteData, 0, byteData.Length);
  33. }
  34. var result = "";
  35. using (var response = request.GetResponse() as HttpWebResponse)
  36. {
  37. var reader = new StreamReader(response.GetResponseStream());
  38. result = reader.ReadToEnd();
  39. }
  40. return result;
  41. } catch (Exception ex)
  42. {
  43. Logger.Instance.Error("调用Post失败,原因:" + ex);
  44. return "";
  45. }
  46. }
  47. /// <summary>
  48. /// 跨域访问
  49. /// </summary>
  50. /// <param name="url"></param>
  51. /// <param name="param"></param>
  52. /// <returns></returns>
  53. public static string Get(string url, int time = 60000)
  54. {
  55. var address = new Uri(url);
  56. var request = WebRequest.Create(address) as HttpWebRequest;
  57. request.Method = "GET";
  58. request.ContentType = "application/json;charset=utf-8"; //"application/x-www-form-urlencoded";
  59. request.Timeout = time;
  60. var result = "";
  61. try
  62. {
  63. using (var response = request.GetResponse() as HttpWebResponse)
  64. {
  65. var reader = new StreamReader(response.GetResponseStream());
  66. result = reader.ReadToEnd();
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. Logger.Instance.Error("调用Get失败,原因:" + ex);
  72. result = "";
  73. }
  74. return result;
  75. }
  76. }
  77. public class WechatToken{
  78. public string access_token { get; set; }
  79. public string openid { get; set; }
  80. public string scope { get; set; }
  81. public string expires_in { get; set; }
  82. }
  83. public class WechatUserinfo {
  84. public string nickname { get; set; }
  85. public string sex { get; set; }
  86. public string province { get; set; }
  87. public string city { get; set; }
  88. public string country { get; set; }
  89. //public string headimgurl { get; set; }
  90. //public string privilege { get; set; }
  91. public string unionid { get; set; }
  92. }
  93. }