软测单独项目
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.

403 lines
16 KiB

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace FangYar.Common
{
/// <summary>
/// Web数据处理帮助类
/// </summary>
public class WebCommonUtil
{
/// <summary>
/// 是否手机或者微信小程序请求信息
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static bool IsPhoneRequest(HttpContext context)
{
try
{
var Request = context.Request;
var str = Request.ServerVariables["HTTP_USER_AGENT"];
if (str.IndexOf("(Linux;Android") > 0)
{
return true;
}
//判断是否PC版小程序
if (str.IndexOf("WindowsWindowsWechat") > 0)
{
return true;
}
}
catch (Exception ex)
{
}
return false;
}
/// <summary>
/// 上级机构转换下级机构ID
/// </summary>
/// <param name="orgId"></param>
/// <returns></returns>
public static string GetOrgIdUpLevelToDownLevel(string orgId)
{
var orgIdCase = orgId.ToLower().Trim();
var _orgid = orgId;
switch (orgIdCase)
{
case "d8dc637b8b984848a63f82a018afaeb0": //西藏总队
_orgid = "734389c7ae364584aea9918693bba3de"; //总队机关
break;
case "f4d9f25b27114172a66a9d04cc158b00": // 拉萨支队
_orgid = "0055c83cf8194051888061fb27971578"; //拉萨机关
break;
case "40a1e65991764d4dac2d826bfbbfc849": // 日喀则支队
_orgid = "a3a158f3ed6a463d92ef65aa58c67bfa"; //日喀则机关
break;
case "5806465fa4e845998f31a10d023f332e": // 昌都支队
_orgid = "335ef5c069954872a50d012b4676393a"; //昌都机关
break;
case "b5b00dfb27a34e0a8bd86270930e02d6": // 林芝支队
_orgid = "1cc32e7245694343b1e5ff41014eee74"; //机关
break;
case "82487f0ebb6848e7ac4abc3d7cbc8504": // 山南支队
_orgid = "0cf00efb5e5f4f668b86e08aff0ea3c4"; //山南机关
break;
case "9a1324ec8b244a7f9d6cbca88cd614fd": // 那曲支队
_orgid = "2a0e0a66602145cfb456b98d92776977"; //机关
break;
case "833b28c12b42495a99a5e41068eb6805": // 阿里支队
_orgid = "ba4e41e82ddd4d819703f0a57f088d4e"; //阿里机关
break;
default:
_orgid = orgId; //返回原值
break;
}
return _orgid;
}
/// <summary>
/// 下级机构转换上级机构ID
/// </summary>
/// <param name="orgId"></param>
/// <returns></returns>
public static string GetOrgIdDownLevelToUpLevel(string orgId)
{
var orgIdCase = orgId.ToLower().Trim();
var _orgid = orgId;
switch (orgIdCase)
{
case "734389c7ae364584aea9918693bba3de": //总队机关
_orgid = "D8DC637B8B984848A63F82A018AFAEB0"; //西藏总队
break;
case "0055c83cf8194051888061fb27971578": // 拉萨机关
_orgid = "f4d9f25b27114172a66a9d04cc158b00"; //拉萨支队
break;
case "a3a158f3ed6a463d92ef65aa58c67bfa": // 日喀则机关
_orgid = "40a1e65991764d4dac2d826bfbbfc849"; //日喀则支队
break;
case "335ef5c069954872a50d012b4676393a": // 昌都机关
_orgid = "5806465fa4e845998f31a10d023f332e"; //昌都支队
break;
case "1cc32e7245694343b1e5ff41014eee74": // 林芝机关
_orgid = "b5b00dfb27a34e0a8bd86270930e02d6"; //林芝支队
break;
case "0cf00efb5e5f4f668b86e08aff0ea3c4": // 山南机关
_orgid = "82487f0ebb6848e7ac4abc3d7cbc8504"; //山南支队
break;
case "2a0e0a66602145cfb456b98d92776977": // 那曲机关
_orgid = "9a1324ec8b244a7f9d6cbca88cd614fd"; //那曲支队
break;
case "ba4e41e82ddd4d819703f0a57f088d4e": // 阿里机关
_orgid = "833b28c12b42495a99a5e41068eb6805"; //阿里支队
break;
default:
_orgid = orgId; //返回原值
break;
}
return _orgid;
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="postUrl">请求地址</param>
/// <param name="paramData">请求参数</param>
/// <returns></returns>
private static string PostWebRequestSend(string postUrl, string paramData)
{
string ret = string.Empty;
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
//webReq.ContentType = "application/json;charset=UTF-8";
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
newStream.Close();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
string str = "Web请求异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "WebUrlRequest"
});
ret = "{\"code\":500,\"success\":false,\"data\":{},\"msg\":\"调用同步接口异常\"}";
}
return ret;
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="postUrl">请求地址</param>
/// <param name="dicParamData">请求参数</param>
/// <returns></returns>
public static string PostWebRequest(string postUrl, Dictionary<string, string> dicParamData)
{
//序列化参数
var jsonParam = JsonConvert.SerializeObject(dicParamData);
return PostWebRequestSend(postUrl, jsonParam);
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="postUrl">请求地址</param>
/// <param name="dicParamData">请求参数</param>
/// <returns></returns>
public static string PostWebRequest(string postUrl, string paramStr)
{
return PostWebRequestSend(postUrl, paramStr);
}
/// <summary>
/// 海康云眸平台API接口
/// </summary>
/// <param name="postUrl">请求地址</param>
/// <param name="paramData">请求参数</param>
/// <returns></returns>
public static string PostYunmouApi(string postUrl, string paramData)
{
string ret = string.Empty;
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
WebRequest webReq = WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
WebResponse response = webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
string str = "Web请求异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "WebUrlRequest"
});
ret = "{\"code\":500,\"success\":false,\"data\":{},\"msg\":\"调用同步接口异常\"}";
}
return ret;
}
/// <summary>
/// Url参数完整拼接方式调用
/// </summary>
/// <param name="contentUrl"></param>
/// <returns></returns>
public static string GetPostUrl(string contentUrl)
{
string ret = "";
try
{
HttpWebRequest reqContent = (HttpWebRequest)WebRequest.Create(contentUrl);
reqContent.Method = "POST";
reqContent.AllowAutoRedirect = false;//服务端重定向。一般设置false
reqContent.ContentType = "application/x-www-form-urlencoded";//数据一般设置这个值,除非是文件上传
HttpWebResponse respContent = (HttpWebResponse)reqContent.GetResponse();
ret = new StreamReader(respContent.GetResponseStream()).ReadToEnd();
}
catch (Exception ex)
{
string str = "Web请求异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "WebUrlRequest"
});
ret = "{\"code\":500,\"success\":false,\"data\":{},\"msg\":\"调用同步接口异常\"}";
}
return ret;
}
/// <summary>
/// 请求参数放入FormData进行Post请求
/// </summary>
/// <param name="urlStr"></param>
/// <param name="map"></param>
/// <returns></returns>
public static string HttpPostFormData(string urlStr, Dictionary<string, string> map)
{
string str = "";
foreach (var item in map)
{
if (!string.IsNullOrWhiteSpace(str))
{
str += "&";
}
str += item.Key + "=" + item.Value;
}
string content = PostWebRequestSend11(urlStr, str);
//try
//{
// ServicePointManager.Expect100Continue = true;
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// //ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
// var mfdc = new System.Net.Http.MultipartFormDataContent();
// mfdc.Headers.Add("ContentType", "multipart/form-data");//声明头部
// foreach (string key in map.Keys)
// {
// mfdc.Add(new System.Net.Http.StringContent(map[key]), key);//参数, 内容在前,参数名称在后
// }
// ServicePointManager.Expect100Continue = true;
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
// //ServicePointManager.ServerCertificateValidationCallback = ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate
// //{
// // return true;
// // //https请求 忽略证书,可以直接访问
// //});
// var clientTask = new System.Net.Http.HttpClient().PostAsync(urlStr, mfdc);//发起异步请求
// clientTask.Wait();//等待请求结果
// if (clientTask.Result.IsSuccessStatusCode)
// {
// //请求正常
// var resultTask = clientTask.Result.Content.ReadAsStringAsync();//异步读取返回内容
// resultTask.Wait();//等读取返回内容
// content = resultTask.Result;//返回内容字符串
// }
// else
// {
// //请求异常
// content = "失败";
// }
//}
//catch (Exception ex)
//{
// content = ex.Message;
// string str = "请求调用异常:" + ex;
// FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
// {
// message = str,
// msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
// path = "WebUrlRequest"
// });
//}
return content;
}
/// <summary>
/// Post请求
/// </summary>
/// <param name="postUrl">请求地址</param>
/// <param name="paramData">请求参数</param>
/// <returns></returns>
private static string PostWebRequestSend11(string postUrl, string paramData)
{
string ret = string.Empty;
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//ServicePointManager.Expect100Continue = true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
////ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
//webReq.ContentType = "application/json;charset=UTF-8";
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
newStream.Close();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
string str = "Web请求异常:" + ex;
FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo()
{
message = str,
msgType = FangYar.Common.EnumLogMsgTypeEnum.Error,
path = "WebUrlRequest"
});
}
return ret;
}
}
}