tijian_tieying/web/dccdc/Common/Deepleo.Weixin.SDK/Helpers/Util.cs
2025-02-20 12:14:39 +08:00

202 lines
7.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Net;
using System.IO;
namespace Deepleo.Weixin.SDK.Helpers
{
public class Util
{
/// <summary>
/// Sha1
/// </summary>
/// <param name="orgStr"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string Sha1(string orgStr, string encode = "UTF-8")
{
var sha1 = new SHA1Managed();
var sha1bytes = System.Text.Encoding.GetEncoding(encode).GetBytes(orgStr);
byte[] resultHash = sha1.ComputeHash(sha1bytes);
string sha1String = BitConverter.ToString(resultHash).ToLower();
sha1String = sha1String.Replace("-", "");
return sha1String;
}
public static string MD5(string encypStr, string charset = "UTF-8")
{
string retStr;
MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
//创建md5对象
byte[] inputBye;
byte[] outputBye;
//使用GB2312编码方式把字符串转化为字节数组
try
{
inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
}
catch
{
inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
}
outputBye = m5.ComputeHash(inputBye);
retStr = System.BitConverter.ToString(outputBye);
retStr = retStr.Replace("-", "").ToUpper();
return retStr;
}
private static string[] strs = new string[]
{
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
};
/// <summary>
/// 创建随机字符串
/// </summary>
/// <returns></returns>
public static string CreateNonce_str()
{
Random r = new Random();
var sb = new StringBuilder();
var length = strs.Length;
for (int i = 0; i < 15; i++)
{
sb.Append(strs[r.Next(length - 1)]);
}
return sb.ToString();
}
/// <summary>
/// 创建时间戳
/// </summary>
/// <returns></returns>
public static long CreateTimestamp()
{
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
/// <summary>
/// html转义
/// </summary>
/// <param name="instr"></param>
/// <returns></returns>
public static string Transfer(string instr)
{
if (instr == null) return "";
return instr.Replace("&", "&amp;").Replace("<", "&lt;")
.Replace(">", "&gt;").Replace("\"", "&quot;");
}
/// <summary>
/// html转义字符串还原
/// </summary>
/// <param name="instr"></param>
/// <returns></returns>
public static string DeTransfer(string instr)
{
if (instr == null) return "";
return instr.Replace("&amp;", "&").Replace("&lt;", "<")
.Replace("&gt;", ">").Replace("&quot;", "\"");
}
/// <summary>
/// FORM表单POST方式上传一个多媒体文件
/// </summary>
/// <param name="url">API URL</param>
/// <param name="typeName"></param>
/// <param name="fileName"></param>
/// <param name="fs"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string HttpRequestPost(string url, string typeName, string fileName, Stream fs, string encoding = "UTF-8")
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Timeout = 10000;
var postStream = new MemoryStream();
#region Form表单文件上传
//通过表单上传文件
string boundary = "----" + DateTime.Now.Ticks.ToString("x");
string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
try
{
var formdata = string.Format(formdataTemplate, typeName, fileName);
var formdataBytes = Encoding.ASCII.GetBytes(postStream.Length == 0 ? formdata.Substring(2, formdata.Length - 2) : formdata);//第一行不需要换行
postStream.Write(formdataBytes, 0, formdataBytes.Length);
//写入文件
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
{
postStream.Write(buffer, 0, bytesRead);
}
}
catch (Exception ex)
{
throw ex;
}
//结尾
var footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
postStream.Write(footer, 0, footer.Length);
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
#endregion
request.ContentLength = postStream != null ? postStream.Length : 0;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.KeepAlive = true;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
#region
if (postStream != null)
{
postStream.Position = 0;
//直接写入流
Stream requestStream = request.GetRequestStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
postStream.Close();//关闭文件访问
}
#endregion
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding)))
{
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
}
/// <summary>
/// 下载
/// </summary>
/// <param name="url"></param>
/// <param name="stream"></param>
public static void Download(string url, Stream stream)
{
WebClient wc = new WebClient();
var data = wc.DownloadData(url);
foreach (var b in data)
{
stream.WriteByte(b);
}
}
}
}