a
This commit is contained in:
72
01.扩展/HandleUtils/Base64Helper.cs
Normal file
72
01.扩展/HandleUtils/Base64Helper.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace HandleUtils
|
||||
{
|
||||
public class Base64Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Base64加密,采用utf8编码方式加密
|
||||
/// </summary>
|
||||
/// <param name="source">待加密的明文</param>
|
||||
/// <returns>加密后的字符串</returns>
|
||||
public static string Base64Encode(string source)
|
||||
{
|
||||
return Base64Encode(Encoding.UTF8, source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64加密
|
||||
/// </summary>
|
||||
/// <param name="encodeType">加密采用的编码方式</param>
|
||||
/// <param name="source">待加密的明文</param>
|
||||
/// <returns></returns>
|
||||
public static string Base64Encode(Encoding encodeType, string source)
|
||||
{
|
||||
string encode = string.Empty;
|
||||
byte[] bytes = encodeType.GetBytes(source);
|
||||
try
|
||||
{
|
||||
encode = Convert.ToBase64String(bytes);
|
||||
}
|
||||
catch
|
||||
{
|
||||
encode = source;
|
||||
}
|
||||
return encode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解密,采用utf8编码方式解密
|
||||
/// </summary>
|
||||
/// <param name="result">待解密的密文</param>
|
||||
/// <returns>解密后的字符串</returns>
|
||||
public static string Base64Decode(string result)
|
||||
{
|
||||
return Base64Decode(Encoding.UTF8, result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解密
|
||||
/// </summary>
|
||||
/// <param name="encodeType">解密采用的编码方式,注意和加密时采用的方式一致</param>
|
||||
/// <param name="result">待解密的密文</param>
|
||||
/// <returns>解密后的字符串</returns>
|
||||
public static string Base64Decode(Encoding encodeType, string result)
|
||||
{
|
||||
string decode = string.Empty;
|
||||
byte[] bytes = Convert.FromBase64String(result);
|
||||
try
|
||||
{
|
||||
decode = encodeType.GetString(bytes);
|
||||
}
|
||||
catch
|
||||
{
|
||||
decode = result;
|
||||
}
|
||||
return decode;
|
||||
}
|
||||
}
|
||||
}
|
||||
149
01.扩展/HandleUtils/EncryptHelper.cs
Normal file
149
01.扩展/HandleUtils/EncryptHelper.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace HandleUtils
|
||||
{
|
||||
public static class EncryptHelper
|
||||
{
|
||||
public static string UrlEncode(string value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(value);
|
||||
return Encoding.UTF8.GetString(UrlEncode(bytes, 0, bytes.Length, alwaysCreateNewReturnValue: false));
|
||||
}
|
||||
|
||||
private static byte[] UrlEncode(byte[] bytes, int offset, int count, bool alwaysCreateNewReturnValue)
|
||||
{
|
||||
byte[] array = UrlEncode(bytes, offset, count);
|
||||
if (!alwaysCreateNewReturnValue || array == null || array != bytes)
|
||||
{
|
||||
return array;
|
||||
}
|
||||
|
||||
return (byte[])array.Clone();
|
||||
}
|
||||
|
||||
private static byte[] UrlEncode(byte[] bytes, int offset, int count)
|
||||
{
|
||||
if (!ValidateUrlEncodingParameters(bytes, offset, count))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int num = 0;
|
||||
int num2 = 0;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
char c = (char)bytes[offset + i];
|
||||
if (c == ' ')
|
||||
{
|
||||
num++;
|
||||
}
|
||||
else if (!IsUrlSafeChar(c))
|
||||
{
|
||||
num2++;
|
||||
}
|
||||
}
|
||||
|
||||
if (num == 0 && num2 == 0)
|
||||
{
|
||||
if (offset == 0 && bytes.Length == count)
|
||||
{
|
||||
return bytes;
|
||||
}
|
||||
|
||||
byte[] array = new byte[count];
|
||||
Buffer.BlockCopy(bytes, offset, array, 0, count);
|
||||
return array;
|
||||
}
|
||||
|
||||
byte[] array2 = new byte[count + num2 * 2];
|
||||
int num3 = 0;
|
||||
for (int j = 0; j < count; j++)
|
||||
{
|
||||
byte b = bytes[offset + j];
|
||||
char c2 = (char)b;
|
||||
if (IsUrlSafeChar(c2))
|
||||
{
|
||||
array2[num3++] = b;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c2 == ' ')
|
||||
{
|
||||
array2[num3++] = 43;
|
||||
continue;
|
||||
}
|
||||
|
||||
array2[num3++] = 37;
|
||||
array2[num3++] = (byte)IntToHex((b >> 4) & 0xF);
|
||||
array2[num3++] = (byte)IntToHex(b & 0xF);
|
||||
}
|
||||
|
||||
return array2;
|
||||
}
|
||||
|
||||
private static char IntToHex(int n)
|
||||
{
|
||||
if (n <= 9)
|
||||
{
|
||||
return (char)(n + 48);
|
||||
}
|
||||
|
||||
return (char)(n - 10 + 65);
|
||||
}
|
||||
|
||||
private static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count)
|
||||
{
|
||||
if (bytes == null && count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (bytes == null)
|
||||
{
|
||||
throw new ArgumentNullException("bytes");
|
||||
}
|
||||
|
||||
if (offset < 0 || offset > bytes.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("offset");
|
||||
}
|
||||
|
||||
if (count < 0 || offset + count > bytes.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("count");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsUrlSafeChar(char ch)
|
||||
{
|
||||
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (ch)
|
||||
{
|
||||
case '!':
|
||||
case '(':
|
||||
case ')':
|
||||
case '*':
|
||||
case '-':
|
||||
case '.':
|
||||
case '_':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
49
01.扩展/HandleUtils/HandleUtils.csproj
Normal file
49
01.扩展/HandleUtils/HandleUtils.csproj
Normal file
@@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{D6A5E5A0-7529-4FFA-9F9D-B2C610919BF6}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>HandleUtils</RootNamespace>
|
||||
<AssemblyName>HandleUtils</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Base64Helper.cs" />
|
||||
<Compile Include="EncryptHelper.cs" />
|
||||
<Compile Include="WebHelper.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
36
01.扩展/HandleUtils/Properties/AssemblyInfo.cs
Normal file
36
01.扩展/HandleUtils/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("HandleUtils")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("HandleUtils")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2024")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("a386e924-0477-49bf-a3dd-82390edf75c7")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
|
||||
//通过使用 "*",如下所示:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
99
01.扩展/HandleUtils/WebHelper.cs
Normal file
99
01.扩展/HandleUtils/WebHelper.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Security;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
|
||||
namespace HandleUtils
|
||||
{
|
||||
public static class WebHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// post请求
|
||||
/// </summary>
|
||||
/// <param name="url">请求地址</param>
|
||||
/// <param name="postData">请求数据</param>
|
||||
/// <param name="certificate2">证书</param>
|
||||
/// <returns></returns>
|
||||
public static string DoPost(string url, string postData, X509Certificate2 certificate2)
|
||||
{
|
||||
try
|
||||
{
|
||||
string result = string.Empty;
|
||||
|
||||
HttpWebRequest request = null;
|
||||
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request = WebRequest.Create(url) as HttpWebRequest;
|
||||
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
||||
request.ProtocolVersion = HttpVersion.Version11;
|
||||
// 这里设置了协议类型。
|
||||
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2;
|
||||
request.KeepAlive = false;
|
||||
ServicePointManager.CheckCertificateRevocationList = true;
|
||||
ServicePointManager.DefaultConnectionLimit = 100;
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
request = (HttpWebRequest)WebRequest.Create(url);
|
||||
}
|
||||
|
||||
//string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
||||
|
||||
////证书
|
||||
//var keystorefile = baseDirectory + @"\bin\ISSUE\testISSUE.pfx";
|
||||
//var key = "123456";
|
||||
//var cer = new X509Certificate2(keystorefile, key);
|
||||
|
||||
if (certificate2 != null)
|
||||
request.ClientCertificates.Add(certificate2);
|
||||
|
||||
request.Method = "POST"; //使用get方式发送数据
|
||||
request.ContentType = "application/json;charset=utf-8";
|
||||
|
||||
byte[] data = Encoding.UTF8.GetBytes(postData);
|
||||
Stream newStream = request.GetRequestStream();
|
||||
newStream.Write(data, 0, data.Length);
|
||||
newStream.Close();
|
||||
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
Stream stream = response.GetResponseStream();
|
||||
|
||||
using (StreamReader sr = new StreamReader(stream))
|
||||
{
|
||||
result = sr.ReadToEnd();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// post请求
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="postData"></param>
|
||||
/// <returns></returns>
|
||||
public static string DoPost(string url, string postData)
|
||||
{
|
||||
return DoPost(url, postData, null);
|
||||
}
|
||||
|
||||
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
||||
{
|
||||
if (errors == SslPolicyErrors.None)
|
||||
return true; //总是接受
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user