|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- using Newtonsoft.Json.Linq;
-
- using System;
- using System.Collections.Generic;
- 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;
-
- namespace Common.system
- {
- /// <summary>
- /// HttpHelper
- /// 创建人:赵耀
- /// </summary>
- public class HttpHelper
- {
- public static void init_Request(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 = null;
- if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
- {
- ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
- request = WebRequest.Create(url) as HttpWebRequest;
- 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 = HttpHelper.GetHttpWebRequest(downloadUrl);
- HttpHelper.init_Request(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 Get(string url, IDictionary<string, string> param)
- {
- List<string> paramBuilder = new List<string>();
- foreach (KeyValuePair<string, string> item in param)
- {
- paramBuilder.Add(string.Format("{0}={1}", item.Key, item.Value));
- }
- url = string.Format("{0}?{1}", url.TrimEnd('?'), string.Join(",", paramBuilder.ToArray()));
- return Get(url);
- }
- public static string Get(string url)
- {
- try
- {
- HttpWebRequest request = GetHttpWebRequest(url);
- if (request != null)
- {
- string retval = null;
- init_Request(ref request);
- using (WebResponse Response = request.GetResponse())
- {
- using (StreamReader reader = new System.IO.StreamReader(Response.GetResponseStream(), System.Text.Encoding.UTF8))
- {
- retval = reader.ReadToEnd();
- }
- }
- return retval;
- }
- }
- catch
- {
-
- }
- return null;
- }
- public static string Post(string url, string data)
- {
- try
- {
- HttpWebRequest request = GetHttpWebRequest(url);
- if (request != null)
- {
- string retval = null;
- init_Request(ref request);
- request.Method = "POST";
- request.ServicePoint.Expect100Continue = false;
- request.ContentType = "application/json; charset=utf-8";
- request.Timeout = 5000;
- byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(data);
- request.ContentLength = bytes.Length;
- using (Stream stream = request.GetRequestStream())
- {
- stream.Write(bytes, 0, bytes.Length);
- }
- using (WebResponse response = request.GetResponse())
- {
- using (StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))
- {
- retval = reader.ReadToEnd();
- }
- }
- return retval;
- }
- }
- catch
- {
-
- }
- return null;
- }
-
- /// <summary>
- ///调用Post接口
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public static string HttpPost(string body, string Url)
- {
- try
- {
- Encoding encoding = Encoding.UTF8;
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
- request.Method = "POST";
- request.Accept = "text/html,application/xhtml+xml,*/*";
- request.ContentType = "application/json";
-
- byte[] buffer = encoding.GetBytes(body);
- request.ContentLength = buffer.Length;
- request.GetRequestStream().Write(buffer, 0, buffer.Length);
-
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
- {
- return reader.ReadToEnd();
- }
- }
- catch (Exception ex)
- {
- // LogHelper.Instance.Debug($"POST接口连接失败,请求参数:{ex.Message}");
- return "POST报错:" + ex.Message;
- }
- }
- /// <summary>
- /// Post Http请求
- /// </summary>
- /// <param name="url">请求地址</param>
- /// <param name="postData">传输数据</param>
- /// <param name="timeout">超时时间</param>
- /// <param name="contentType">媒体格式</param>
- /// <param name="encode">编码</param>
- /// <returns>泛型集合</returns>
- public static T PostAndRespSignle<T>(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)
- {
- // webRequest.Headers.Add("Authorization", "Bearer " + "SportApiAuthData");
- HttpWebResponse webResponse = null;
- Stream responseStream = null;
- Stream requestStream = null;
- StreamReader streamReader = null;
- try
- {
- string respstr = GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType);
- return JsonHelper.JsonToObj<T>(respstr);
- }
- catch (Exception)
- {
- }
- finally
- {
- if (responseStream != null)
- {
- responseStream.Dispose();
- }
-
- if (webResponse != null)
- {
- webResponse.Close();
- }
-
- if (requestStream != null)
- {
- requestStream.Dispose();
- }
-
- if (streamReader != null)
- {
- streamReader.Dispose();
- }
- }
- }
- return default(T);
- }
- private static string GetStreamReader(string url, Stream responseStream, Stream requestStream, StreamReader streamReader, WebResponse webResponse, int timeout, string encode, string postData, string contentType)
- {
- try
- {
- byte[] data = Encoding.GetEncoding(encode).GetBytes(postData);
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
- webRequest.Method = "POST";
- webRequest.ContentType = contentType + ";" + encode;
- webRequest.ContentLength = data.Length;
- webRequest.Timeout = timeout;
- requestStream = webRequest.GetRequestStream();
- requestStream.Write(data, 0, data.Length);
- webResponse = (HttpWebResponse)webRequest.GetResponse();
- responseStream = webResponse.GetResponseStream();
- if (responseStream == null) { return ""; }
- streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode));
- return streamReader.ReadToEnd();
- }
- catch (Exception)
- {
- return null;
- }
- }
- /// <summary>
- /// Cookie
- /// </summary>
- public static CookieContainer cc = new CookieContainer();
- /// <summary>
- /// Get请求
- /// </summary>
- /// <param name="serviceaddress">请求地址</param>
- /// <param name="strcontent">头</param>
- /// <param name="contenttype">参数</param>
- /// <param name="header">token</param>
- /// <returns></returns>
- public static JObject GetFunction(string serviceaddress, string strcontent, string contenttype, string header, int Timeout = 5000)
- {
- try
- {
- string serviceAddress = serviceaddress + strcontent;
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
- WebHeaderCollection myWebHeaderCollection = request.Headers;
- request.CookieContainer = cc;
- if (header != "")
- {
- myWebHeaderCollection.Add("access_token:" + header);
- }
-
- request.Method = "GET";
- request.ContentType = contenttype;
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
- string encoding = response.ContentEncoding;
- if (encoding == null || encoding.Length < 1)
- {
- encoding = "UTF-8"; //默认编码
- }
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
- string retString = reader.ReadToEnd();
-
- //解析josn
- JObject jo = JObject.Parse(retString);
- return jo;
- }
- catch (Exception)
- {
- return null;
- }
- }
-
- /// <summary>
- /// Post请求
- /// </summary>
- /// <param name="serviceaddress">请求地址</param>
- /// <param name="contenttype">头 application/x-www-form-urlencoded</param>
- /// <param name="strcontent">参数</param>
- /// <param name="header">token</param>
- /// <returns></returns>
- 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;
-
- string strContent = strcontent;
- using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
- {
- dataStream.Write(strContent);
- dataStream.Close();
- }
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- string encoding = response.ContentEncoding;
- if (encoding == null || encoding.Length < 1)
- {
- encoding = "UTF-8"; //默认编码
- }
- 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;
- }
-
- }
-
- /// <summary>
- /// 上传方法
- /// </summary>
- /// <param name="url">webapi地址</param>
- /// <param name="files">本地文件路径,单文件默认为string[0]</param>
- /// <param name="token"></param>
- /// <param name="formFields">参数可不传</param>
- /// <returns></returns>
- public static JObject UploadFilesToRemoteUrl(string url, string[] files, string token, NameValueCollection formFields = null)
- {
- string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.ContentType = "multipart/form-data; boundary=" +
- boundary;
- request.Method = "POST";
- request.KeepAlive = true;
-
- Stream memStream = new System.IO.MemoryStream();
-
- byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
- boundary + "\r\n");
- byte[] endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
- boundary + "--");
- WebHeaderCollection myWebHeaderCollection = request.Headers;
- request.CookieContainer = cc;
- if (token != "")
- {
- myWebHeaderCollection.Add("access_token:" + token);
- }
-
- string formdataTemplate = "\r\n--" + boundary +
- "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
- if (formFields != null)
- {
- foreach (string key in formFields.Keys)
- {
- string formitem = string.Format(formdataTemplate, key, formFields[key]);
- byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
- memStream.Write(formitembytes, 0, formitembytes.Length);
- }
- }
-
- string headerTemplate =
- "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
- "Content-Type: application/octet-stream\r\n\r\n";
-
- for (int i = 0; i < files.Length; i++)
- {
- memStream.Write(boundarybytes, 0, boundarybytes.Length);
- string header = string.Format(headerTemplate, "file", files[i]);
- byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
-
- memStream.Write(headerbytes, 0, headerbytes.Length);
-
- using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
- {
- byte[] buffer = new byte[1024];
- int bytesRead = 0;
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- memStream.Write(buffer, 0, bytesRead);
- }
- }
- }
-
- //memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
- request.ContentLength = memStream.Length;
-
- using (Stream requestStream = request.GetRequestStream())
- {
- memStream.Position = 0;
- byte[] tempBuffer = new byte[memStream.Length];
- memStream.Read(tempBuffer, 0, tempBuffer.Length);
- memStream.Close();
- requestStream.Write(tempBuffer, 0, tempBuffer.Length);
- }
-
- using (WebResponse response = request.GetResponse())
- {
- Stream stream2 = response.GetResponseStream();
- StreamReader reader2 = new StreamReader(stream2);
- JObject a = JObject.Parse(reader2.ReadToEnd());
- return a;
- }
- }
-
- /// <summary>
- /// HttpWebRequest 下载
- /// </summary>
- /// <param name="url">URI</param>
- /// <returns></returns>
- 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();
- long length = webRespon.ContentLength;
- if (webStream == null)
- {
- return false;
- }
- int lengthint = Convert.ToInt32(length);
- //int num = lengthint / 1024;
- //int count = 0;
- //M_DownloadInfo.totalCount = lengthint / 1024;
- //M_DownloadInfo.downloadCount = 0;
-
- 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)
- {
- //LogHelper.WriteErrLog("请求失败:" + ex.Message, ex);
- return false;
- }
- }
-
- /// <summary>
- /// 上传文件流
- /// 创建人:赵耀
- /// 创建时间:2020年9月5日
- /// </summary>
- /// <param name="url">URL</param>
- /// <param name="databyte">文件数据流</param>
- /// <param name="filename">文件名</param>
- /// <param name="formFields">参数 可不传</param>
- public static JObject UploadRequestflow(string url, byte[] databyte, string filename, NameValueCollection formFields = null)
- {
- JObject jobResult = 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();
-
- //获取服务器端的响应
- using (HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse())
- {
- Stream receiveStream = response.GetResponseStream();
- StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
- string returnValue = readStream.ReadToEnd();
- jobResult = JObject.Parse(returnValue);
- response.Close();
- readStream.Close();
- //MessageWindow.Show(returnValue);
- //LogHelper.WriteInfoLog("【文件上传】" + returnValue);
- }
- return jobResult;
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【文件上传】" + ex.Message, ex);
- return null;
- }
- finally
- {
- httpReq = null;
- }
- }
- /// <summary>
- /// Post Http请求
- /// </summary>
- /// <param name="url"></param>
- /// <param name="postData"></param>
- /// <param name="timeout"></param>
- /// <param name="contentType"></param>
- /// <param name="encode"></param>
- /// <returns>响应流字符串</returns>
- public static string PostAndRespStr(string url, string postData = "", int timeout = 5000, string contentType = "application/json;", string encode = "UTF-8")
- {
- if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null)
- {
- HttpWebResponse webResponse = null;
- Stream responseStream = null;
- Stream requestStream = null;
- StreamReader streamReader = null;
- try
- {
- return GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType);
- }
- catch (Exception)
- {
- }
- finally
- {
- if (responseStream != null)
- {
- responseStream.Dispose();
- }
-
- if (webResponse != null)
- {
- webResponse.Close();
- }
-
- if (requestStream != null)
- {
- requestStream.Dispose();
- }
-
- if (streamReader != null)
- {
- streamReader.Dispose();
- }
- }
- }
- return null;
- }
- }
- }
|