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.

138 rivejä
4.3 KiB

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;
/// <summary>
/// WX_Util 的摘要说明
/// </summary>
public class WX_Util
{
public static string getRandomStr()
{
return Guid.NewGuid().ToString().Replace("-", "");
}
public static string extract(string str, string key)
{
SortedDictionary<string, string> map = new SortedDictionary<string, string>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(str);
XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
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<string, string> 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<string, string> map, string key)
{
string xml = "<xml>";
xml += "<sign>" + sign(map, key) + "</sign>";
foreach (KeyValuePair<string, string> pair in map)
{
if (pair.Value != null)
{
if (pair.Value.GetType() == typeof(int))
{
xml += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">";
}
else if (pair.Value.GetType() == typeof(string))
{
xml += "<" + pair.Key + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">";
}
}
}
xml += "</xml>";
return xml;
}
public static string sign(SortedDictionary<string, string> map, string key)
{
string strb = "";
foreach (KeyValuePair<string, string> 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: 在此处添加构造函数逻辑
//
}
}