namespace Common.system
{
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
public class ZHttpUtil
{
public static string tokenKey = "";
public static string tokenValue = "";
public static string userId = "";
public static string version = "";
public static bool isSt;
public static void InitRequest(ref System.Net.HttpWebRequest request)
{
request.Accept = "text/json,*/*;q=0.5";
request.Headers.Add("Accept-Charset", "utf-8;q=0.7,*;q=0.7");
request.Headers.Add("Accept-Encoding", "gzip, deflate, x-gzip, identity; q=0.9");
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
request.Timeout = 8000;
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
public static System.Net.HttpWebRequest GetHttpWebRequest(string url)
{
HttpWebRequest request;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
request = WebRequest.Create(url) as HttpWebRequest;
if (request != null)
{
request.ProtocolVersion = HttpVersion.Version10;
}
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
return request;
}
public static WebResponse Download(string downloadUrl, long from, long to, string method)
{
for (int i = 0; i < 10; i++)
{
try
{
HttpWebRequest request = GetHttpWebRequest(downloadUrl);
InitRequest(ref request);
request.Accept = "text/json,*/*;q=0.5";
request.AddRange(from, to);
request.Headers.Add("Accept-Charset", "utf-8;q=0.7,*;q=0.7");
request.Headers.Add("Accept-Encoding", "gzip, deflate, x-gzip, identity; q=0.9");
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
request.Timeout = 120000;
request.Method = method;
request.KeepAlive = false;
request.ContentType = "application/json; charset=utf-8";
return request.GetResponse();
}
catch (Exception)
{
Thread.Sleep(100);
}
}
throw new Exception("已断开网络!请检查网络连接后重试下载!");
}
public static string GetStr(string url)
{
try
{
HttpWebRequest request = GetHttpWebRequest(url);
if (request != null)
{
AddHeader(request);
string retval;
InitRequest(ref request);
using (WebResponse response = request.GetResponse())
{
// ReSharper disable once AssignNullToNotNullAttribute
using (StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
{
retval = reader.ReadToEnd();
}
}
return retval;
}
}
catch (Exception)
{
// ignored
}
return null;
}
private static void AddHeader(HttpWebRequest webRequest)
{
webRequest.Headers.Add("Xh-St", isSt ? "true" : "false");
if (!string.IsNullOrEmpty(tokenKey) && !string.IsNullOrEmpty(tokenValue))
{
webRequest.Headers.Add("Xh-Token-Key", tokenKey);
webRequest.Headers.Add("Xh-Token-Value", tokenValue);
}
if (!string.IsNullOrEmpty(userId))
{
webRequest.Headers.Add("Xh-User-Id", userId);
}
webRequest.Headers.Add("Xh-Device", "microlecture_pc_t");
webRequest.Headers.Add("Xh-Version", version);
}
///
/// 调用Post接口
///
///
public static string PostStr(string url, string postData)
{
Stream responseStream = null;
try
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.Accept = "text/html,application/xhtml+xml,*/*";
webRequest.ContentType = "application/json";
string aesPostData = postData;
if (isSt)
{
aesPostData = AesHelper.AesEncrypt(postData, "XINGHUOLIAOYUAN7");
}
AddHeader(webRequest);
byte[] buffer = encoding.GetBytes(aesPostData);
webRequest.ContentLength = buffer.Length;
webRequest.GetRequestStream()
.Write
(
buffer,
0,
buffer.Length
);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
isSt = webResponse.GetResponseHeader("Xh-St") == "true";
responseStream = webResponse.GetResponseStream();
if (responseStream == null) { return ""; }
using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
catch (Exception)
{
return "";
}
finally
{
if (responseStream != null)
{
responseStream.Dispose();
}
}
}
///
/// Post Http请求
///
/// 请求地址
/// 传输数据
/// 超时时间
/// 媒体格式
/// 编码
/// 泛型集合
public static T PostSignle
(
string url,
int timeout = 5000,
string postData = "",
string contentType = "application/json;",
string encode = "UTF-8"
) where T : class
{
if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null)
{
try
{
string respstr = PostStr(url, postData);
return JsonHelper.JsonToObj(respstr);
}
catch (Exception)
{
// ignored
}
}
return default;
}
///
/// Cookie
///
public static CookieContainer cc = new CookieContainer();
///
/// Post请求
///
/// 请求地址
/// 头 application/x-www-form-urlencoded
/// 参数
/// token
///
///
public static JObject PostFunction
(
string serviceaddress,
string contenttype,
string strcontent,
string header,
int timeout = 5000
)
{
try
{
string serviceAddress = serviceaddress;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
WebHeaderCollection myWebHeaderCollection = request.Headers;
request.CookieContainer = cc;
//myWebHeaderCollection.Add("Accept", "*/*");
if (header != "")
{
myWebHeaderCollection.Add("access_token:" + header);
}
request.Timeout = timeout;
request.Method = "POST";
request.ContentType = contenttype;
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(strcontent);
dataStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
// ReSharper disable once AssignNullToNotNullAttribute
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
retString = retString.Replace("\\r\\n", "");
//解析josn
JObject jo = JObject.Parse(retString);
return jo;
}
catch (Exception ex)
{
LogHelper.WriteErrLog("请求失败(若网络无问题,请重置winsock,解决方法:以管理员方式打开cmd执行 netsh winsock reset 后重启)", ex);
return null;
}
}
///
/// HttpWebRequest 下载
///
/// URI
///
///
///
public static bool GetDataGetHtml
(
string url,
string filePath,
string header
)
{
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
//httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "GET";
//httpWebRequest.ContentType = "image/jpeg";
//对发送的数据不使用缓存
httpWebRequest.AllowWriteStreamBuffering = false;
//httpWebRequest.Timeout = 300000;
httpWebRequest.ServicePoint.Expect100Continue = false;
WebHeaderCollection myWebHeaderCollection = httpWebRequest.Headers;
//myWebHeaderCollection.Add("Accept", "*/*");
if (header != "")
{
myWebHeaderCollection.Add("access_token:" + header);
}
HttpWebResponse webRespon = (HttpWebResponse)httpWebRequest.GetResponse();
Stream webStream = webRespon.GetResponseStream();
if (webStream == null)
{
return false;
}
Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
byte[] bArr = new byte[1024];
int size = webStream.Read(bArr, 0, bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = webStream.Read(bArr, 0, bArr.Length);
//M_DownloadInfo.downloadCount = M_DownloadInfo.downloadCount + 1;
}
webRespon.Close();
stream.Close();
webStream.Close();
return true;
}
catch (Exception)
{
return false;
}
}
///
/// 上传文件流
/// 创建人:赵耀
/// 创建时间:2020年9月5日
///
/// URL
/// 文件数据流
/// 文件名
/// 参数 可不传
public static JObject UploadRequestflow(string url, byte[] databyte, string filename, NameValueCollection formFields = null)
{
// 时间戳,用做boundary
string boundary = "-----" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
//根据uri创建HttpWebRequest对象
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
httpReq.ContentType = "multipart/form-data; boundary=" + boundary;
httpReq.Method = "POST";
httpReq.KeepAlive = true;
//httpReq.Timeout = 300000;
//httpReq.AllowWriteStreamBuffering = false; //对发送的数据不使用缓存
httpReq.Credentials = CredentialCache.DefaultCredentials;
try
{
Stream postStream = httpReq.GetRequestStream();
//参数
const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
if (formFields != null)
{
foreach (string key in formFields.Keys)
{
postStream.Write
(
boundarybytes,
0,
boundarybytes.Length
);
string formitem = string.Format
(
formdataTemplate,
key,
formFields[key]
);
byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
postStream.Write
(
formitembytes,
0,
formitembytes.Length
);
}
}
postStream.Write
(
boundarybytes,
0,
boundarybytes.Length
);
const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
//文件头
string header = string.Format
(
headerTemplate,
"file",
filename
);
byte[] headerbytes = Encoding.UTF8.GetBytes(header);
postStream.Write
(
headerbytes,
0,
headerbytes.Length
);
//文件流
postStream.Write
(
databyte,
0,
databyte.Length
);
//结束边界
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
postStream.Write
(
boundaryBytes,
0,
boundaryBytes.Length
);
postStream.Close();
//获取服务器端的响应
JObject jobResult = null;
using (HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse())
{
Stream receiveStream = response.GetResponseStream();
if (receiveStream != null)
{
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string returnValue = readStream.ReadToEnd();
jobResult = JObject.Parse(returnValue);
response.Close();
readStream.Close();
}
}
return jobResult;
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【文件上传】" + ex.Message, ex);
return null;
}
}
///
/// 获取公网IP和地址,失败则获取局域网IP
///
/// IP地址
/// 地址
///
public static bool GetAddressIp(out string addressIp, out string address)
{
addressIp = "";
address = "";
try
{
//请求搜狐获取公网IP,获取失败后获取局域网IP
WebRequest request = WebRequest.Create("http://pv.sohu.com/cityjson?ie=utf-8");
request.Timeout = 10000;
WebResponse response = request.GetResponse();
Stream resStream = response.GetResponseStream();
if (resStream != null)
{
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
string htmlinfo = sr.ReadToEnd();
string jsonStr = htmlinfo.Substring(htmlinfo.IndexOf("{", StringComparison.Ordinal), htmlinfo.LastIndexOf("}", StringComparison.Ordinal) - htmlinfo.IndexOf("{", StringComparison.Ordinal) + 1);
JObject obj = JObject.Parse(jsonStr);
addressIp = obj["cip"]?.ToString();
address = obj["cname"]?.ToString();
resStream.Close();
sr.Close();
}
return true;
}
catch (Exception)
{
//获取本地局域网IP
foreach (System.Net.IPAddress ipAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (ipAddress.AddressFamily.ToString() == "InterNetwork")
{
addressIp = ipAddress.ToString();
}
}
return false;
}
}
}
}