|
|
using System;using System.Collections.Generic;using System.Data;using System.IO;using System.Linq;using System.Net;using System.Reflection;using System.Text;using System.Threading.Tasks;
namespace NFine.Data.Extensions{ public class PublicMethod { /// <summary>
/// 获取单据号
/// </summary>
/// <param name="tbName"></param>
/// <param name="colName"></param>
/// <param name="Pre"></param>
/// <param name="numLen"></param>
/// <returns></returns>
public static string GetSerialCode( string tbName, string colName, string Pre, int numLen) { string sql = "EXEC Addins_GetSerialCode '0001','{0}','{1}','{2}',{3}"; sql = string.Format(sql, new object[] { tbName, colName, Pre, numLen }); return SqlHelper.ExecuteScalar(sql).ToString(); }
///<summary>
///copy相同字段的数据
public static D Mapper<D, S>(S s) {
D d = Activator.CreateInstance<D>();
try {
var Types = s.GetType();//获得类型
var Typed = typeof(D);
foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段
{
foreach (PropertyInfo dp in Typed.GetProperties()) {
if (dp.Name == sp.Name)//判断属性名是否相同
{
dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性
}
}
}
}
catch (Exception ex) {
throw ex;
}
return d;
}
/// <summary>
/// Get接口查询方法
/// </summary>
/// <param name="apiName"></param>
/// <param name="url"></param>
/// <returns></returns>
public static string QueryPostparamsService(string apiName, string url) { string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "Get"; //req.Headers.Add()
try { HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } } catch (Exception ex) { throw new Exception(apiName + "调用失败," + ex.Message); } return result; } /// <summary>
///
/// </summary>
/// <param name="apiName"></param>
/// <param name="url"></param>
/// <param name="body"></param>
/// <returns></returns>
public static string HttpPost(string apiName, string url, string body) { try { Encoding encoding = Encoding.UTF8; System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); request.Method = "POST"; if (url.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)) { request.ProtocolVersion = HttpVersion.Version11; request.ServerCertificateValidationCallback = (a, b, c, d) => true; } //log.Debug(url + Environment.NewLine + body);
request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
request.ContentType = "application/json; charset=utf-8"; // request.ContentType = "text/html, application/xhtml+xml";
byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (System.Net.WebException ex) { throw new Exception(apiName + "调用失败," + ex.Message); } }
/// <summary>
/// post接口调用方法
/// </summary>
/// <param name="apiName"></param>
/// <param name="url"></param>
/// <param name="body"></param>
/// <param name="webHeader"></param>
/// <returns></returns>
public static string HttpPost(string apiName, string url, string body, WebHeaderCollection webHeader = null) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; try { Encoding encoding = Encoding.UTF8; System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); request.Method = "POST"; if (url.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)) { request.ProtocolVersion = HttpVersion.Version11; request.ServerCertificateValidationCallback = (a, b, c, d) => true; } request.Accept = "application/json, text/javascript, */*"; request.ContentType = "application/json; charset=utf-8"; var timeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimeOut"]);//超时时间
if (timeout > 0) request.Timeout = timeout; if (webHeader != null) request.Headers = webHeader;
byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (System.Net.WebException ex) { throw new Exception(apiName + "调用失败," + ex.Message); } }
/// <summary>
/// 获取ID
/// </summary>
/// <returns></returns>
public static string GetNewid() { string sql = "select newid() AS ID"; return SqlHelper.ExecuteScalar(sql).ToString();
}
}}
|