|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Xml.Serialization;
|
|
|
using System.IO;
|
|
|
using System.Xml;
|
|
|
using System.Net;
|
|
|
using System.IO.Compression;
|
|
|
using System.Net.Http;
|
|
|
using DSWeb.Areas.MvcShipping.DB;
|
|
|
using DSWeb.Areas.Import.Models.ImportTrade;
|
|
|
using System.Threading.Tasks;
|
|
|
using static DSWeb.MvcShipping.DAL.MsOpSeaeEdiDAL.MsOpSeaeEdiDAL;
|
|
|
using System.Collections.Specialized;
|
|
|
using com.sun.org.apache.xerces.@internal.xs;
|
|
|
using DSWeb.Areas.CommMng.Models;
|
|
|
|
|
|
namespace DSWeb.Areas.MvcShipping.Comm
|
|
|
{
|
|
|
public static class WebRequestHelper
|
|
|
{
|
|
|
public static string DoGet(string url)
|
|
|
{
|
|
|
string responseString = "";//post返回的结果
|
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
req.Method = "GET";
|
|
|
req.ContentLength = 0;
|
|
|
|
|
|
var response = req.GetResponse();
|
|
|
Stream streamResponse = response.GetResponseStream();
|
|
|
StreamReader streamRead = new StreamReader(streamResponse);
|
|
|
responseString = streamRead.ReadToEnd();
|
|
|
response.Close();
|
|
|
streamRead.Close();
|
|
|
|
|
|
return responseString;
|
|
|
}
|
|
|
|
|
|
public static string DoGet(string url, NameValueCollection stringDict, NameValueCollection Headers = null)
|
|
|
{
|
|
|
string responseString = "";//post返回的结果
|
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
req.Method = "GET";
|
|
|
req.ContentLength = 0;
|
|
|
|
|
|
|
|
|
|
|
|
var response = req.GetResponse();
|
|
|
Stream streamResponse = response.GetResponseStream();
|
|
|
StreamReader streamRead = new StreamReader(streamResponse);
|
|
|
responseString = streamRead.ReadToEnd();
|
|
|
response.Close();
|
|
|
streamRead.Close();
|
|
|
|
|
|
return responseString;
|
|
|
}
|
|
|
|
|
|
public static string DoGet(string url, Dictionary<string, string> dic, int timeout = 10000, NameValueCollection Headers = null)
|
|
|
{
|
|
|
string responseString = "";//post返回的结果
|
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
req.Method = "GET";
|
|
|
req.Timeout = timeout;
|
|
|
|
|
|
if (Headers != null)
|
|
|
{
|
|
|
foreach (string key in Headers.Keys)
|
|
|
{
|
|
|
req.Headers.Add(key, Headers[key]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (dic.Count > 0)
|
|
|
{
|
|
|
string strContent = string.Empty;
|
|
|
foreach (var item in dic)
|
|
|
{
|
|
|
if (strContent.Length > 0)
|
|
|
{
|
|
|
strContent += "&";
|
|
|
}
|
|
|
strContent += $"{item.Key}={item.Value}";
|
|
|
}
|
|
|
|
|
|
byte[] postBytes = Encoding.UTF8.GetBytes(strContent);
|
|
|
req.ContentType = "application/x-www-form-urlencoded";
|
|
|
req.ContentLength = postBytes.Length;
|
|
|
Stream stream = req.GetRequestStream();
|
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
|
stream.Close();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var response = req.GetResponse();
|
|
|
Stream streamResponse = response.GetResponseStream();
|
|
|
StreamReader streamRead = new StreamReader(streamResponse);
|
|
|
responseString = streamRead.ReadToEnd();
|
|
|
response.Close();
|
|
|
streamRead.Close();
|
|
|
|
|
|
return responseString;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static string DoGet_Param_Header(string url, Dictionary<string, string> dic, int timeout = 10000, NameValueCollection Headers = null)
|
|
|
{
|
|
|
string responseString = "";//post返回的结果
|
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
|
|
|
if (dic.Count > 0) {
|
|
|
url += "?";
|
|
|
|
|
|
var paramstr = "";
|
|
|
|
|
|
foreach (var item in dic) {
|
|
|
if (paramstr != "") paramstr += "&";
|
|
|
paramstr += $"{item.Key}={item.Value}";
|
|
|
}
|
|
|
|
|
|
url+= paramstr;
|
|
|
}
|
|
|
|
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
req.Method = "GET";
|
|
|
req.Timeout = timeout;
|
|
|
|
|
|
if (Headers != null)
|
|
|
{
|
|
|
foreach (string key in Headers.Keys)
|
|
|
{
|
|
|
req.Headers.Add(key, Headers[key]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
req.ContentLength = 0;
|
|
|
|
|
|
var response = req.GetResponse();
|
|
|
Stream streamResponse = response.GetResponseStream();
|
|
|
StreamReader streamRead = new StreamReader(streamResponse);
|
|
|
responseString = streamRead.ReadToEnd();
|
|
|
response.Close();
|
|
|
streamRead.Close();
|
|
|
|
|
|
return responseString;
|
|
|
}
|
|
|
|
|
|
public static string DoPost(string url, string json, int timeout = 10000)
|
|
|
{
|
|
|
string responseString = "";//post返回的结果
|
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
req.Method = "POST";
|
|
|
req.Timeout = timeout;
|
|
|
req.Headers.Add("Accept-Encoding", "gzip, deflate");
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(json))
|
|
|
{
|
|
|
byte[] postBytes = Encoding.UTF8.GetBytes(json);
|
|
|
req.ContentType = "application/json; charset=utf-8";
|
|
|
req.ContentLength = Encoding.UTF8.GetByteCount(json);
|
|
|
Stream stream = req.GetRequestStream();
|
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
|
stream.Close();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
var response = req.GetResponse();
|
|
|
responseString = GetResponseBody((HttpWebResponse)response);
|
|
|
return responseString;
|
|
|
}
|
|
|
public static string DoPost(string url, object sendObject, int timeout = 10000)
|
|
|
{
|
|
|
var json = DSWeb.MvcShipping.Helper.JsonConvert.Serialize(sendObject);
|
|
|
return DoPost(url, json, timeout);
|
|
|
}
|
|
|
|
|
|
public static string DoPost_JSON_Header(string url, string json, int timeout = 10000, NameValueCollection Headers = null)
|
|
|
{
|
|
|
string responseString = "";//post返回的结果
|
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
req.Method = "POST";
|
|
|
req.Timeout = timeout;
|
|
|
req.Headers.Add("Accept-Encoding", "gzip, deflate");
|
|
|
|
|
|
if (Headers != null)
|
|
|
{
|
|
|
foreach (string key in Headers.Keys)
|
|
|
{
|
|
|
req.Headers.Add(key, Headers[key]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(json))
|
|
|
{
|
|
|
byte[] postBytes = Encoding.UTF8.GetBytes(json);
|
|
|
req.ContentType = "application/json; charset=utf-8";
|
|
|
req.ContentLength = Encoding.UTF8.GetByteCount(json);
|
|
|
Stream stream = req.GetRequestStream();
|
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
|
stream.Close();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
var response = req.GetResponse();
|
|
|
responseString = GetResponseBody((HttpWebResponse)response);
|
|
|
return responseString;
|
|
|
}
|
|
|
|
|
|
public static string DoPost(string url, Dictionary<string, string> dic, int timeout = 10000, NameValueCollection Headers = null)
|
|
|
{
|
|
|
string responseString = "";//post返回的结果
|
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
req.Method = "POST";
|
|
|
req.Timeout = timeout;
|
|
|
req.Headers.Add("Accept-Encoding", "gzip, deflate");
|
|
|
|
|
|
if (Headers != null)
|
|
|
{
|
|
|
foreach (string key in Headers.Keys)
|
|
|
{
|
|
|
req.Headers.Add(key, Headers[key]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (dic.Count > 0)
|
|
|
{
|
|
|
string strContent = string.Empty;
|
|
|
foreach (var item in dic)
|
|
|
{
|
|
|
if (strContent.Length > 0)
|
|
|
{
|
|
|
strContent += "&";
|
|
|
}
|
|
|
strContent += $"{item.Key}={item.Value}";
|
|
|
}
|
|
|
|
|
|
byte[] postBytes = Encoding.UTF8.GetBytes(strContent);
|
|
|
req.ContentType = "application/x-www-form-urlencoded";
|
|
|
req.ContentLength = postBytes.Length;
|
|
|
Stream stream = req.GetRequestStream();
|
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
|
stream.Close();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
var response = req.GetResponse();
|
|
|
responseString = GetResponseBody((HttpWebResponse)response);
|
|
|
return responseString;
|
|
|
}
|
|
|
|
|
|
private static string GetResponseBody(HttpWebResponse response)
|
|
|
{
|
|
|
string responseBody = string.Empty;
|
|
|
if (response.ContentEncoding.ToLower().Contains("gzip"))
|
|
|
{
|
|
|
using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
|
|
|
{
|
|
|
using (StreamReader reader = new StreamReader(stream))
|
|
|
{
|
|
|
responseBody = reader.ReadToEnd();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else if (response.ContentEncoding.ToLower().Contains("deflate"))
|
|
|
{
|
|
|
using (DeflateStream stream = new DeflateStream(
|
|
|
response.GetResponseStream(), CompressionMode.Decompress))
|
|
|
{
|
|
|
using (StreamReader reader =
|
|
|
new StreamReader(stream, Encoding.UTF8))
|
|
|
{
|
|
|
responseBody = reader.ReadToEnd();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
using (Stream stream = response.GetResponseStream())
|
|
|
{
|
|
|
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
|
|
{
|
|
|
responseBody = reader.ReadToEnd();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return responseBody;
|
|
|
}
|
|
|
|
|
|
public static string HttpPostData(string url, int timeOut, string fileKeyName,
|
|
|
string filePath, string filename, NameValueCollection stringDict, NameValueCollection Headers=null)
|
|
|
{
|
|
|
string responseContent;
|
|
|
var request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
MsMultiPartFormData form = new MsMultiPartFormData();
|
|
|
|
|
|
foreach (string key in stringDict.Keys)
|
|
|
{
|
|
|
form.AddFormField(key, stringDict[key]);
|
|
|
}
|
|
|
|
|
|
FileStream file = new FileStream(filePath, FileMode.Open);
|
|
|
byte[] bb = new byte[file.Length];
|
|
|
file.Read(bb, 0, (int)file.Length);
|
|
|
file.Close();
|
|
|
form.AddStreamFile(fileKeyName, filename, bb);
|
|
|
form.PrepareFormData();
|
|
|
|
|
|
request.ContentType = "multipart/form-data; boundary=" + form.Boundary;
|
|
|
request.Method = "POST";
|
|
|
|
|
|
if (Headers != null)
|
|
|
{
|
|
|
foreach (string key in Headers.Keys)
|
|
|
{
|
|
|
request.Headers.Add(key, Headers[key]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Stream stream = request.GetRequestStream();
|
|
|
foreach (var b in form.GetFormData())
|
|
|
{
|
|
|
stream.WriteByte(b);
|
|
|
}
|
|
|
stream.Close();
|
|
|
WebResponse response = request.GetResponse();
|
|
|
using (var httpStreamReader = new StreamReader(response.GetResponseStream(),
|
|
|
Encoding.GetEncoding("utf-8")))
|
|
|
{
|
|
|
responseContent = httpStreamReader.ReadToEnd();
|
|
|
}
|
|
|
|
|
|
return responseContent;
|
|
|
}
|
|
|
|
|
|
public static string HttpPost(string url, int timeOut, NameValueCollection stringDict, NameValueCollection Headers = null)
|
|
|
{
|
|
|
string responseContent;
|
|
|
var request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
MsMultiPartFormData form = new MsMultiPartFormData();
|
|
|
|
|
|
foreach (string key in stringDict.Keys)
|
|
|
{
|
|
|
form.AddFormField(key, stringDict[key]);
|
|
|
}
|
|
|
|
|
|
form.PrepareFormData();
|
|
|
|
|
|
request.ContentType = "multipart/form-data; boundary=" + form.Boundary;
|
|
|
request.Method = "POST";
|
|
|
|
|
|
if (Headers != null)
|
|
|
{
|
|
|
foreach (string key in Headers.Keys)
|
|
|
{
|
|
|
request.Headers.Add(key, Headers[key]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Stream stream = request.GetRequestStream();
|
|
|
foreach (var b in form.GetFormData())
|
|
|
{
|
|
|
stream.WriteByte(b);
|
|
|
}
|
|
|
stream.Close();
|
|
|
WebResponse response = request.GetResponse();
|
|
|
using (var httpStreamReader = new StreamReader(response.GetResponseStream(),
|
|
|
Encoding.GetEncoding("utf-8")))
|
|
|
{
|
|
|
responseContent = httpStreamReader.ReadToEnd();
|
|
|
}
|
|
|
return responseContent;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// http post请求
|
|
|
/// </summary>
|
|
|
/// <param name="url">地址</param>
|
|
|
/// <param name="parameter">入参</param>
|
|
|
/// <param name="token"></param>
|
|
|
/// <param name="headers">头信息</param>
|
|
|
/// <returns></returns>
|
|
|
/// <exception cref="Exception"></exception>
|
|
|
/// , string token ="", Dictionary<string, string> headers = null
|
|
|
public static async Task<string> PostAsyncNoHeaders(string url, string parameter)
|
|
|
{
|
|
|
using (var client = new HttpClient())
|
|
|
{
|
|
|
//var data = new StringContent("param1=value1¶m2=value2", Encoding.UTF8, "application/x-www-form-urlencoded");
|
|
|
var data = new StringContent(parameter, Encoding.UTF8, "application/x-www-form-urlencoded");
|
|
|
//_logger.LogWarning($"data:{data.ToJsonString()};parameter:{parameter}");
|
|
|
// 添加header参数
|
|
|
//client.DefaultRequestHeaders.Add("Authorization", "Bearer your_token_here");
|
|
|
//foreach (var header in headers)
|
|
|
//{
|
|
|
// client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
|
|
//}
|
|
|
//_logger.LogWarning($"header:{client.DefaultRequestHeaders.ToJsonString()}");
|
|
|
|
|
|
try
|
|
|
{
|
|
|
client.Timeout = TimeSpan.FromSeconds(5);
|
|
|
|
|
|
var response = await client.PostAsync(url, data);
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
var responseBody = await response.Content.ReadAsStringAsync();
|
|
|
return responseBody;
|
|
|
}
|
|
|
catch (HttpRequestException e)
|
|
|
{
|
|
|
throw new Exception(e.Message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
public static class XmlHelper
|
|
|
{
|
|
|
private static string NAMESPACE = "http://HCHX.Schemas.";
|
|
|
private static void XmlSerializeInternal ( Stream stream, object o, Encoding encoding,string ip)
|
|
|
{
|
|
|
if (o == null)
|
|
|
throw new ArgumentNullException("o");
|
|
|
if (encoding == null)
|
|
|
throw new ArgumentNullException("encoding");
|
|
|
|
|
|
XmlSerializer serializer = new XmlSerializer(o.GetType());
|
|
|
|
|
|
XmlWriterSettings settings = new XmlWriterSettings();
|
|
|
settings.Indent = true;
|
|
|
settings.NewLineChars = "\r\n";
|
|
|
settings.Encoding = encoding;
|
|
|
settings.IndentChars = " ";
|
|
|
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
|
|
|
|
|
|
using (XmlWriter writer = XmlWriter.Create(stream, settings))
|
|
|
{
|
|
|
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
|
|
|
namespaces.Add("ns", ip);
|
|
|
|
|
|
serializer.Serialize(writer, o, namespaces);
|
|
|
writer.Close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 将一个对象序列化为XML字符串
|
|
|
/// </summary>
|
|
|
/// <param name="o">要序列化的对象</param>
|
|
|
/// <param name="encoding">编码方式</param>
|
|
|
/// <param name="nameSpace">命名空间</param>
|
|
|
/// <returns>序列化产生的XML字符串</returns>
|
|
|
public static string XmlSerialize ( object o, Encoding encoding, string nameSpace,string ip )
|
|
|
{
|
|
|
NAMESPACE = NAMESPACE + nameSpace;
|
|
|
using (MemoryStream stream = new MemoryStream())
|
|
|
{
|
|
|
XmlSerializeInternal(stream, o, encoding,ip);
|
|
|
|
|
|
stream.Position = 0;
|
|
|
using (StreamReader reader = new StreamReader(stream, encoding))
|
|
|
{
|
|
|
return reader.ReadToEnd();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 将一个对象按XML序列化的方式写入到一个文件
|
|
|
/// </summary>
|
|
|
/// <param name="o">要序列化的对象</param>
|
|
|
/// <param name="path">保存文件路径</param>
|
|
|
/// <param name="encoding">编码方式</param>
|
|
|
public static void XmlSerializeToFile ( object o, string path, Encoding encoding, string nameSpace ,string ip)
|
|
|
{
|
|
|
NAMESPACE = NAMESPACE + nameSpace;
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
|
using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
|
|
|
{
|
|
|
XmlSerializeInternal(file, o, encoding,ip);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 从XML字符串中反序列化对象
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T">结果对象类型</typeparam>
|
|
|
/// <param name="s">包含对象的XML字符串</param>
|
|
|
/// <param name="encoding">编码方式</param>
|
|
|
/// <returns>反序列化得到的对象</returns>
|
|
|
public static T XmlDeserialize<T> ( string s, Encoding encoding )
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(s))
|
|
|
throw new ArgumentNullException("s");
|
|
|
if (encoding == null)
|
|
|
throw new ArgumentNullException("encoding");
|
|
|
|
|
|
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
|
|
|
using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s)))
|
|
|
{
|
|
|
using (StreamReader sr = new StreamReader(ms, encoding))
|
|
|
{
|
|
|
return (T)mySerializer.Deserialize(sr);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 读入一个文件,并按XML的方式反序列化对象。
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T">结果对象类型</typeparam>
|
|
|
/// <param name="path">文件路径</param>
|
|
|
/// <param name="encoding">编码方式</param>
|
|
|
/// <returns>反序列化得到的对象</returns>
|
|
|
public static T XmlDeserializeFromFile<T> ( string path, Encoding encoding )
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
throw new ArgumentNullException("path");
|
|
|
if (encoding == null)
|
|
|
throw new ArgumentNullException("encoding");
|
|
|
|
|
|
string xml = File.ReadAllText(path, encoding);
|
|
|
return XmlDeserialize<T>(xml, encoding);
|
|
|
}
|
|
|
}
|
|
|
}
|