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.

137 lines
4.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Web;
  9. using System.Xml;
  10. /// <summary>
  11. /// WX_Util 的摘要说明
  12. /// </summary>
  13. public class WX_Util
  14. {
  15. public static string getRandomStr()
  16. {
  17. return Guid.NewGuid().ToString().Replace("-", "");
  18. }
  19. public static string extract(string str, string key)
  20. {
  21. SortedDictionary<string, string> map = new SortedDictionary<string, string>();
  22. XmlDocument xmlDoc = new XmlDocument();
  23. xmlDoc.LoadXml(str);
  24. XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
  25. XmlNodeList nodes = xmlNode.ChildNodes;
  26. foreach (XmlNode xn in nodes)
  27. {
  28. XmlElement xe = (XmlElement)xn;
  29. if (key.Equals(xe.Name))
  30. {
  31. return xe.InnerText;
  32. }
  33. }
  34. return "";
  35. }
  36. public static ReturnBean submit(SortedDictionary<string, string> map, string key)
  37. {
  38. string str = null;
  39. HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.mch.weixin.qq.com/pay/unifiedorder");
  40. request.Method = "POST";
  41. Stream requestStream = request.GetRequestStream();
  42. StreamWriter streamWriter = new StreamWriter(requestStream);
  43. streamWriter.Write(getXML(map, key));
  44. streamWriter.Flush();
  45. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  46. Stream responseStream = response.GetResponseStream();
  47. StreamReader streamReader = new StreamReader(responseStream);
  48. str = streamReader.ReadToEnd();
  49. streamWriter.Close();
  50. streamReader.Close();
  51. WX_Util.log(str);
  52. return new ReturnBean(str);
  53. }
  54. public static void log(string str)
  55. {
  56. System.IO.FileStream file = new System.IO.FileStream(HttpContext.Current.Server.MapPath("/upload/log.txt"), System.IO.FileMode.Append);
  57. System.IO.StreamWriter writer = new System.IO.StreamWriter(file);
  58. writer.WriteLine(str + "\n\r");
  59. writer.Close();
  60. }
  61. public static string getXML(SortedDictionary<string, string> map, string key)
  62. {
  63. string xml = "<xml>";
  64. xml += "<sign>" + sign(map, key) + "</sign>";
  65. foreach (KeyValuePair<string, string> pair in map)
  66. {
  67. if (pair.Value != null)
  68. {
  69. if (pair.Value.GetType() == typeof(int))
  70. {
  71. xml += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">";
  72. }
  73. else if (pair.Value.GetType() == typeof(string))
  74. {
  75. xml += "<" + pair.Key + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">";
  76. }
  77. }
  78. }
  79. xml += "</xml>";
  80. return xml;
  81. }
  82. public static string sign(SortedDictionary<string, string> map, string key)
  83. {
  84. string strb = "";
  85. foreach (KeyValuePair<string, string> pair in map)
  86. {
  87. if (pair.Key != "sign" && pair.Value != "")
  88. {
  89. strb += pair.Key + "=" + pair.Value + "&";
  90. }
  91. }
  92. strb += "key=" + key;
  93. return md5(strb);
  94. }
  95. public static string md5(string str)
  96. {
  97. var md5 = MD5.Create();
  98. var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
  99. var sb = new StringBuilder();
  100. foreach (byte b in bs)
  101. {
  102. sb.Append(b.ToString("x2"));
  103. }
  104. //所有字符转为大写
  105. return sb.ToString().ToUpper();
  106. }
  107. public static string getRequestStr(HttpRequest request)
  108. {
  109. System.IO.Stream s = request.InputStream;
  110. int count = 0;
  111. byte[] buffer = new byte[1024];
  112. StringBuilder builder = new StringBuilder();
  113. while ((count = s.Read(buffer, 0, 1024)) > 0)
  114. {
  115. builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
  116. }
  117. s.Flush();
  118. s.Close();
  119. s.Dispose();
  120. return builder.ToString();
  121. }
  122. public WX_Util()
  123. {
  124. //
  125. // TODO: 在此处添加构造函数逻辑
  126. //
  127. }
  128. }