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请求
{
///
/// 该类实现客户端http 同步请求
/// 支持环境 -.net4.0/-.net4.5
/// 创建人:奉友福
///
public class HttpRequestHelper
{
#region 私有变量
#endregion
#region 公用函数
///
/// GET 同步请求
/// 创建人:奉友福
/// 创建时间:2020-11-19
///
/// 请求地址
/// 超时时间设置,默认5秒
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;
}
///
/// POST 同步请求
/// 创建人:奉友福
/// 创建时间:2020-11-19
///
/// 请求地址
/// 请求数据
///
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;
}
///
/// GET 同步请求
///
/// 地址
/// 头
/// 内容
///
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;
}
///
/// Post请求带Token
/// 2021-2-2 by dulf
///
///
///
///
///
///
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("请求异常:" + ex.Message);
}
return "";
}
#endregion
}
}