2025-03-27 15:05:14 +08:00

237 lines
9.2 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.Data;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.Script.Serialization;
using HZQR.Common;
using NJ = Newtonsoft.Json;
using NJL = Newtonsoft.Json.Linq;
namespace SuperMap.RealEstate.Finance.Common
{
public class Common
{
public const string charSet = "1,2,3,4,5,6,7,8,9";
#region -> get,post传参的值
public static string Request(string key)
{
var v = HttpContext.Current.Request.Form[key];
if (v == null)
{
v = HttpContext.Current.Request.QueryString[key];
}
return v ?? string.Empty;
}
#endregion
#region ->
public static bool GetBillState(string fpdm, string fphm, string kprq, string je, string jym, string BillAppKey, string ServiceUrl)
{
bool Flag = false;
try
{
//fpdm:发票代码,fphm:发票号码,kprq:开票日期,je:金额(非专票随便填个值),jym:校验码后六位(专票随便填个值)
string[] arg = { fpdm, fphm, kprq, je, jym, BillAppKey, null };
string reString = WSHelper.InvokeWebService(ServiceUrl, "BillInfoQuery", arg).ToString();
NJL.JObject _JObject = (NJL.JObject)NJ.JsonConvert.DeserializeObject(reString);
if (_JObject["BillInfo"].ToString() != "[]")
{
Flag = true;
}
}
catch (Exception ex)
{
Flag = false;
}
return Flag;
}
#endregion
#region ->
/// <summary>
/// 生成验证码
/// <param name="n">位数</param>
/// <returns>验证码字符串</returns>
private string CreateRandomCode(int n)
{
string[] CharArray = charSet.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < n; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(CharArray.Length - 1);
if (temp == t)
{
return CreateRandomCode(n);
}
temp = t;
randomCode += CharArray[t];
}
return randomCode;
}
#endregion
#region ->
/// <summary>
/// 将DateTime类型转换为long类型
/// </summary>
/// <param name="dt">时间</param>
/// <returns></returns>
public static long ConvertDataTimeLong(DateTime dt)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
TimeSpan toNow = dt.Subtract(dtStart);
long timeStamp = toNow.Ticks;
timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 7));
return timeStamp;
}
/// <summary>
/// 将Long类型转换为DateTime类型
/// </summary>
/// <param name="d">long</param>
/// <returns></returns>s
public static DateTime ConvertLongDateTime(long d)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(d + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime dtResult = dtStart.Add(toNow);
return dtResult;
}
#endregion
#region md5 16
public static string CreateMD5(string text)
{
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
//换成utf8后对于中文加密也适用
byte[] output = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(text));
string pass2md5 = BitConverter.ToString(output, 4, 8).Replace("-", "");
//pass2md5 = pass2md5.ToUpper();
return pass2md5;
}
#endregion
#region -> Json DataTable数据集合
/// <summary>
/// Json 字符串 转换为 DataTable数据集合
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static DataTable ToDataTable(string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if (arrayList.Count > 0)
{
foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count == 0)
{
result = dataTable;
return result;
}
//Columns
if (dataTable.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
}
//Rows
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch
{
}
result = dataTable;
return result;
}
#endregion
#region -> HTTP报文请求
/// <summary>
/// HTTP报文请求
/// </summary>
/// <param name="postDataStr">请求参数</param>
/// <param name="RequestUrl">请求url</param>
/// <param name="ContentType">资源的媒体类型默认application/x-www-form-urlencoded</param>
/// <param name="timeout">超时时间TCP连接建立的时间和整个请求完成的时间超出了设置的时间就抛出异常程序中断</param>
/// <param name="securityProtocol">服务器通信的默认安全协议众马对账通信需使用Tls12,WCF默认通道即可</param>
/// <returns></returns>
public static string HttpUrlPost(string postDataStr, string RequestUrl,
string ContentType = "application/x-www-form-urlencoded", int timeout = 0)
{
try
{
CookieContainer cookieContainer = new CookieContainer();
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postDataStr);
// 设置提交的相关参数
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = WebRequest.Create(RequestUrl) as HttpWebRequest;
if (timeout > 0)
{
request.Timeout = timeout * 1000;
}
request.Method = "POST";
request.KeepAlive = false;
request.ContentType = ContentType;
request.CookieContainer = cookieContainer;
request.ContentLength = postBytes.Length;
using (System.IO.Stream reqStream = request.GetRequestStream())
{
reqStream.Write(postBytes, 0, postBytes.Length);
}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
//在这里对接收到的页面内容进行处理
//直到request.GetResponse()程序才开始向目标网页发送post请求
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
string val = reader.ReadToEnd();
return val;
}
}
catch (Exception ex)
{
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex, "Post请求", "Post请求" + postDataStr + "" + RequestUrl);
return ex.ToString();
}
}
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
#endregion
}
}