|
@@ -1,8 +1,11 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
+using System.Linq;
|
|
|
using System.Net;
|
|
|
+using System.Net.Http;
|
|
|
using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
using System.Xml;
|
|
|
using wms.dto.response;
|
|
|
using wms.util.Ext;
|
|
@@ -12,7 +15,9 @@ namespace wms.util.Http
|
|
|
public class HttpUtil
|
|
|
{
|
|
|
public static string ERPWebServerPath = string.Empty;
|
|
|
+
|
|
|
#region Get请求
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// Get请求
|
|
|
/// </summary>
|
|
@@ -47,9 +52,9 @@ namespace wms.util.Http
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
+ #endregion Get请求
|
|
|
|
|
|
- #region POST请求
|
|
|
+ #region POST请求
|
|
|
|
|
|
/// <summary>
|
|
|
/// 发起HTTP请求
|
|
@@ -59,7 +64,7 @@ namespace wms.util.Http
|
|
|
/// <param name="timeOut">超时时间</param>
|
|
|
/// <param name="encode">编码方式</param>
|
|
|
/// <param name="contentType">内容格式</param>
|
|
|
- public static string PostRequest(string remoteUrl, string postData, int timeOut = 30000, string encode = "UTF-8", string contentType = "application/json",string requestid ="",string sourcecode ="",string servicecode = "")
|
|
|
+ public static string PostRequest(string remoteUrl, string postData, int timeOut = 30000, string encode = "UTF-8", string contentType = "application/json", string requestid = "", string sourcecode = "", string servicecode = "")
|
|
|
{
|
|
|
var result = string.Empty;
|
|
|
try
|
|
@@ -76,7 +81,6 @@ namespace wms.util.Http
|
|
|
mRequest.Headers["requestId"] = requestid;
|
|
|
mRequest.Headers["sourceCode"] = sourcecode;
|
|
|
mRequest.Headers["serviceCode"] = servicecode;
|
|
|
-
|
|
|
}
|
|
|
//请求流
|
|
|
Stream requestStream = mRequest.GetRequestStream();
|
|
@@ -84,7 +88,7 @@ namespace wms.util.Http
|
|
|
requestStream.Close();
|
|
|
//响应流
|
|
|
var mResponse = (HttpWebResponse)mRequest.GetResponse();
|
|
|
-
|
|
|
+
|
|
|
var responseStream = mResponse.GetResponseStream();
|
|
|
Encoding code = Encoding.GetEncoding(encode);
|
|
|
if (responseStream != null)
|
|
@@ -102,7 +106,7 @@ namespace wms.util.Http
|
|
|
}
|
|
|
if (!string.IsNullOrEmpty(requestid) && mResponse.Headers["esbCode "] != "000000" && string.IsNullOrEmpty(result))
|
|
|
{
|
|
|
- return new SRes() { ResCode = 500, ResMsg = "esbDesc:" + mResponse.Headers["esbDesc"],Memo1 = "esbCode:"+ mResponse.Headers["esbCode"] }.ToCamelCaseString();
|
|
|
+ return new SRes() { ResCode = 500, ResMsg = "esbDesc:" + mResponse.Headers["esbDesc"], Memo1 = "esbCode:" + mResponse.Headers["esbCode"] }.ToCamelCaseString();
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
@@ -112,6 +116,69 @@ namespace wms.util.Http
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ private static readonly HttpClient _httpClient = new HttpClient();
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 发起HTTP请求
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="remoteUrl">接口地址</param>
|
|
|
+ /// <param name="postData">请求参数</param>
|
|
|
+ /// <param name="encode">编码方式</param>
|
|
|
+ /// <param name="contentType">内容格式</param>
|
|
|
+ /// <param name="requestid">请求ID</param>
|
|
|
+ /// <param name="sourcecode">来源代码</param>
|
|
|
+ /// <param name="servicecode">服务代码</param>
|
|
|
+ public static async Task<string> PostRequestAsync(string remoteUrl, string postData, string encode = "UTF-8", string contentType = "application/json", string requestid = "", string sourcecode = "", string servicecode = "")
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var content = new StringContent(postData, Encoding.GetEncoding(encode), contentType);
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ if (!string.IsNullOrEmpty(requestid))
|
|
|
+ {
|
|
|
+ content.Headers.Add("requestId", requestid);
|
|
|
+ content.Headers.Add("sourceCode", sourcecode);
|
|
|
+ content.Headers.Add("serviceCode", servicecode);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送异步POST请求
|
|
|
+ using (var response = await _httpClient.PostAsync(remoteUrl, content))
|
|
|
+ {
|
|
|
+ response.EnsureSuccessStatusCode();
|
|
|
+
|
|
|
+ // 读取响应内容
|
|
|
+ var result = await response.Content.ReadAsStringAsync();
|
|
|
+
|
|
|
+ if (!string.IsNullOrEmpty(requestid) && response.Headers.TryGetValues("esbCode", out var esbCodes) && !esbCodes.Contains("000000") && string.IsNullOrEmpty(result))
|
|
|
+ {
|
|
|
+ return new SRes() { ResCode = 500, ResMsg = "esbDesc:" + response.Headers.GetValues("esbDesc").FirstOrDefault(), Memo1 = "esbCode:" + esbCodes.FirstOrDefault() }.ToCamelCaseString();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 不等待的异步HTTP请求
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="remoteUrl">接口地址</param>
|
|
|
+ /// <param name="postData">请求参数</param>
|
|
|
+ /// <param name="encode">编码方式</param>
|
|
|
+ /// <param name="contentType">内容格式</param>
|
|
|
+ /// <param name="requestid">请求ID</param>
|
|
|
+ /// <param name="sourcecode">来源代码</param>
|
|
|
+ /// <param name="servicecode">服务代码</param>
|
|
|
+ public static void FireAndForgetPostRequest(string remoteUrl, string postData, string encode = "UTF-8", string contentType = "application/json", string requestid = "", string sourcecode = "", string servicecode = "")
|
|
|
+ {
|
|
|
+ _ = PostRequestAsync(remoteUrl, postData, encode, contentType, requestid, sourcecode, servicecode);
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 通过POST提交方式获取XML数据
|
|
|
/// </summary>
|
|
@@ -133,6 +200,7 @@ namespace wms.util.Http
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 发起HTTP请求
|
|
|
/// </summary>
|
|
@@ -186,6 +254,6 @@ namespace wms.util.Http
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
+ #endregion POST请求
|
|
|
}
|
|
|
-}
|
|
|
+}
|