|
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace DataVAPI.Tool.API请求
- {
- /// <summary>
- /// HTTP请求类
- /// </summary>
- public class HttpRequest
- {
- #region 私有变量
- /// <summary>
- /// http请求超时时间设置
- /// 默认值:5秒
- /// </summary>
- private static int Timeout = 5000;
- #endregion
-
- #region 单例模式
- private static HttpRequest _HttpRequest = null;
- public static HttpRequest GetInstance()
- {
- if (_HttpRequest == null)
- {
- _HttpRequest = new HttpRequest();
- }
- return _HttpRequest;
- }
- private HttpRequest()
- {
-
- }
- #endregion
-
- #region 公用函数
- /// <summary>
- /// 函数名称:创建http请求
- /// 创建人:奉友福
- /// 创建时间:2020-11-19
- /// 例如GET 请求: 地址 + "GET"
- /// 例如POST请求: 地址 + "POST" + JSON
- /// </summary>
- /// <param name="url">http请求地址</param>
- /// <param name="requestType">http请求方式:GET/POST</param>
- /// <param name="strjson">http请求附带数据</param>
- /// <returns></returns>
- public HttpWebRequest CreateHttpRequest(string url, string requestType, int _timeout = 5000, params object[] strjson)
- {
- HttpWebRequest request = null;
- const string get = "GET";
- const string post = "POST";
- Timeout = _timeout;
- if (string.Equals(requestType, get, StringComparison.OrdinalIgnoreCase))
- {
- request = CreateGetHttpRequest(url);
- }
- if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
- {
- request = CreatePostHttpRequest(url, strjson[0].ToString());
- }
- return request;
- }
- /// <summary>
- /// http获取数据
- /// </summary>
- /// <param name="response"></param>
- /// <param name="requestType"></param>
- /// <returns></returns>
- public string GetHttpResponse(HttpWebResponse response, string requestType)
- {
- var resultData = string.Empty;
- const string post = "POST";
- string encoding = "UTF-8";
- if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
- {
- encoding = response.ContentEncoding;
- if (encoding == null || encoding.Length < 1)
- encoding = "UTF-8";
- }
- using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)))
- {
- resultData = reader.ReadToEnd();
- }
- return resultData;
- }
- #endregion
-
- #region 私有函数
- /// <summary>
- /// http+GET请求
- /// </summary>
- /// <param name="url">请求地址</param>
- /// <returns>请求结果</returns>
- private static HttpWebRequest CreateGetHttpRequest(string url)
- {
- var getrequest = WebRequest.Create(url) as HttpWebRequest;
- getrequest.Method = "GET";
- getrequest.Timeout = Timeout;
- getrequest.ContentType = "application/json;charset=UTF-8";
- getrequest.Proxy = null;
- getrequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
- return getrequest;
- }
- /// <summary>
- /// http+POST请求
- /// </summary>
- /// <param name="url">请求地址</param>
- /// <param name="postData"></param>
- /// <returns>请求结果</returns>
- private static HttpWebRequest CreatePostHttpRequest(string url, string postData)
- {
- var postrequest = WebRequest.Create(url) as HttpWebRequest;
- //postrequest.KeepAlive = false;
- postrequest.Timeout = Timeout;
- postrequest.Method = "POST";
- postrequest.ContentType = "application/json;charset=UTF-8";
- //postrequest.ContentLength = postData.Length;
- //postrequest.AllowWriteStreamBuffering = false;
- //StreamWriter writer = new StreamWriter(postrequest.GetRequestStream(), Encoding.UTF8);
- //writer.Write(postData);
- //writer.Flush();
- byte[] data = Encoding.UTF8.GetBytes(postData);
- using (Stream reqStream = postrequest.GetRequestStream())
- {
- reqStream.Write(data, 0, data.Length);
- reqStream.Close();
- }
- return postrequest;
- }
-
- public static HttpWebRequest CreateDeleteHttpRequest(string url, string postJson, int _timeout = 5000)
- {
- var deleteRequest = WebRequest.Create(url) as HttpWebRequest;
- deleteRequest.Timeout = _timeout;
- deleteRequest.Method = "DELETE";
- deleteRequest.ContentType = "application/json;charset=UTF-8";
- byte[] data = Encoding.UTF8.GetBytes(postJson);
- using (Stream reqStream = deleteRequest.GetRequestStream())
- {
- reqStream.Write(data, 0, data.Length);
- reqStream.Close();
- }
- return deleteRequest;
- }
- #endregion
- }
- }
|