|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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 同步请求
- /// 支持环境 -.net4.0/-.net4.5
- /// 创建人:奉友福
- /// </summary>
- public class HttpRequestHelper
- {
- #region 私有变量
- #endregion
-
- #region 公用函数
- /// <summary>
- /// GET 同步请求
- /// 创建人:奉友福
- /// 创建时间:2020-11-19
- /// </summary>
- /// <param name="url">请求地址</param>
- /// <returns>超时时间设置,默认5秒</returns>
- public static string HttpGetRequest(string url, int _timeout = 2000)
- {
- string resultData = string.Empty;
-
- try
- {
- WebClient wc = new WebClient();
- byte[] bytes = wc.DownloadData(url);
- string s = Encoding.UTF8.GetString(bytes);
- return s;
- }
- catch (Exception e)
- {
- throw e;
- }
- return "";
-
- try
- {
- var getrequest = HttpRequest.GetInstance().CreateHttpRequest(url, "GET", _timeout);
- var getreponse = getrequest.GetResponse() as HttpWebResponse;
- resultData = HttpRequest.GetInstance().GetHttpResponse(getreponse, "GET");
- }
- catch (Exception)
- {
- throw;
- }
- return resultData;
- }
- /// <summary>
- /// POST 同步请求
- /// 创建人:奉友福
- /// 创建时间:2020-11-19
- /// </summary>
- /// <param name="url">请求地址</param>
- /// <param name="PostJsonData">请求数据</param>
- /// <returns></returns>
- public static string HttpPostRequest(string url, string PostJsonData, int _timeout = 2000)
- {
- string resultData = string.Empty;
- try
- {
- var postrequest = HttpRequest.GetInstance().CreateHttpRequest(url, "POST", _timeout, PostJsonData);
- var postreponse = postrequest.GetResponse() as HttpWebResponse;
- resultData = HttpRequest.GetInstance().GetHttpResponse(postreponse, "POST");
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- return resultData;
- }
-
- public static string HttpDeleteRequest(string url, string PostJsonData, int _timeout = 10000)
- {
- string resultData = string.Empty;
- try
- {
- var deleteRequest = HttpRequest.CreateDeleteHttpRequest(url, PostJsonData, _timeout);
- var deleteReponse = deleteRequest.GetResponse() as HttpWebResponse;
- using (StreamReader reader = new StreamReader(deleteReponse.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
- {
- resultData = reader.ReadToEnd();
- }
- }
- catch (Exception ex)
- {
- }
- return resultData;
- }
-
- /// <summary>
- /// GET 同步请求
- /// </summary>
- /// <param name="url">地址</param>
- /// <param name="head">头</param>
- /// <param name="headInfo">内容</param>
- /// <returns></returns>
- public static string GetHttpGetResponseWithHead(string url, HttpRequestHeader head, string headInfo)
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = "GET";
- request.ContentType = "application/json;charset=UTF-8";
- request.Timeout = 6000;
- request.Headers.Set(head, headInfo);
- StreamReader sr = null;
- HttpWebResponse response = null;
- Stream stream = null;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- stream = response.GetResponseStream();
- sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
- var resultData = sr.ReadToEnd();
- return resultData;
- }
- catch (Exception ex)
- {
- Console.WriteLine(url + " 访问失败:" + ex.Message);
- //return ex.Message;
- }
- finally
- {
- if (response != null)
- {
- response.Dispose();
- }
- if (stream != null)
- {
- stream.Dispose();
- }
- if (sr != null)
- {
- sr.Dispose();
- }
- }
- return null;
- }
-
-
-
- /// <summary>
- /// Post请求带Token
- /// 2021-2-2 by dulf
- /// </summary>
- /// <param name="url"></param>
- /// <param name="head"></param>
- /// <param name="headInfo"></param>
- /// <param name="postParam"></param>
- /// <returns></returns>
- public static string HttpPostResponseWithHead(string url, HttpRequestHeader head, string headInfo, string postParam, int Timeout = 6000)
- {
- string resultData = string.Empty;
- try
- {
- var postrequest = WebRequest.Create(url) as HttpWebRequest;
- postrequest.Timeout = Timeout;
- postrequest.Method = "POST";
- postrequest.ContentType = "application/json;charset=UTF-8";
- postrequest.Headers.Set(head, headInfo);
- byte[] data = Encoding.UTF8.GetBytes(postParam);
- using (Stream reqStream = postrequest.GetRequestStream())
- {
- reqStream.Write(data, 0, data.Length);
- var postreponse = postrequest.GetResponse() as HttpWebResponse;
- resultData = HttpRequest.GetInstance().GetHttpResponse(postreponse, "POST");
- reqStream.Close();
- }
- return resultData;
- }
- catch (Exception ex)
- {
- Console.Write("请求<HttpPostResponseWithHead>异常:" + ex.Message);
- }
- return "";
- }
- #endregion
-
- }
- }
|