using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; using System.Web; using System.Xml; /// /// WX_Util 的摘要说明 /// public class WX_Util { public static string getRandomStr() { return Guid.NewGuid().ToString().Replace("-", ""); } public static string extract(string str, string key) { SortedDictionary map = new SortedDictionary(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(str); XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点 XmlNodeList nodes = xmlNode.ChildNodes; foreach (XmlNode xn in nodes) { XmlElement xe = (XmlElement)xn; if (key.Equals(xe.Name)) { return xe.InnerText; } } return ""; } public static ReturnBean submit(SortedDictionary map, string key) { string str = null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.mch.weixin.qq.com/pay/unifiedorder"); request.Method = "POST"; Stream requestStream = request.GetRequestStream(); StreamWriter streamWriter = new StreamWriter(requestStream); streamWriter.Write(getXML(map, key)); streamWriter.Flush(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream); str = streamReader.ReadToEnd(); streamWriter.Close(); streamReader.Close(); WX_Util.log(str); return new ReturnBean(str); } public static void log(string str) { System.IO.FileStream file = new System.IO.FileStream(HttpContext.Current.Server.MapPath("/upload/log.txt"), System.IO.FileMode.Append); System.IO.StreamWriter writer = new System.IO.StreamWriter(file); writer.WriteLine(str + "\n\r"); writer.Close(); } public static string getXML(SortedDictionary map, string key) { string xml = ""; xml += "" + sign(map, key) + ""; foreach (KeyValuePair pair in map) { if (pair.Value != null) { if (pair.Value.GetType() == typeof(int)) { xml += "<" + pair.Key + ">" + pair.Value + ""; } else if (pair.Value.GetType() == typeof(string)) { xml += "<" + pair.Key + ">" + ""; } } } xml += ""; return xml; } public static string sign(SortedDictionary map, string key) { string strb = ""; foreach (KeyValuePair pair in map) { if (pair.Key != "sign" && pair.Value != "") { strb += pair.Key + "=" + pair.Value + "&"; } } strb += "key=" + key; return md5(strb); } public static string md5(string str) { var md5 = MD5.Create(); var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); var sb = new StringBuilder(); foreach (byte b in bs) { sb.Append(b.ToString("x2")); } //所有字符转为大写 return sb.ToString().ToUpper(); } public static string getRequestStr(HttpRequest request) { System.IO.Stream s = request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); return builder.ToString(); } public WX_Util() { // // TODO: 在此处添加构造函数逻辑 // } }