/*************************************************
* 描述:
*
* Author:ys
* Date:2022/4/13 15:24:25
* Update:
* ************************************************/
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace DaHua.Common
{
/// <summary>
/// Http的Get、Post请求封装
/// </summary>
public static class HttpHelper
{
#region 含账号密码的Http请求封装 对应PostMan 【安全认证 Basic Auth】账号密码方式请求
/// <summary>
/// Http的Get请求【含账号密码】
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <returns></returns>
public static string GetHttpGetByNamePwd(string url, string contentType = null)
{
//接口返回报文
string result = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
if (contentType == null)
{
request.ContentType = "application/json;charset=utf-8";
}
else
{
request.ContentType = contentType;
}
//账户
string name = "guest";
//密码
string pwd = "guest";
string bh = "Authorization:Basic";
string header = name + ":" + pwd;
//UTF8标准转码加密
string base64Header = Convert.ToBase64String(Encoding.UTF8.GetBytes(header));
request.Headers.Add("Authorization", "Basic " + base64Header);
HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
result = streamReader.ReadToEnd();
streamReader.Close();
myResponseStream.Close();
}
catch (WebException ex)
{
var errorSr = new StreamReader(ex.Response.GetResponseStream());
result = errorSr.ReadToEnd();
}
return result;
}
/// <summary>
/// Http的Post请求【含账号密码】
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public static string GetHttpPostByNamePwd(string Url, string postDataStr, string contentType = null)
{
//接口返回报文
string result = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
if (contentType == null)
{
request.ContentType = "application/x-www-form-urlencoded";
}
else
{
request.ContentType = contentType;
}
//账户
string name = "502732";
//密码
string pwd = "A6A3F9EDEC9EA47C3279EA3870610DBE";
string bh = "Authorization:Basic";
string header = name + ":" + pwd;
//UTF8标准转码加密
string base64Header = Convert.ToBase64String(Encoding.UTF8.GetBytes(header));
request.Headers.Add("Authorization", "Basic " + base64Header);
byte[] data = Encoding.GetEncoding("utf-8").GetBytes(postDataStr);
request.ContentLength = data.Length;
Stream myRequestStream = request.GetRequestStream();
myRequestStream.Write(data, 0, data.Length);
myRequestStream.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = httpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
result = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
catch (WebException ex)
{
var errorSr = new StreamReader(ex.Response.GetResponseStream());
result = errorSr.ReadToEnd();
}
return result;
}
#endregion 含账号密码的Http请求封装
#region 不含账号密码普通http请求,对应PostMan 直接Json请求
/// <summary>
/// Http的Post请求
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public static string GetHttpPost(string Url, string postDataStr, string contentType = null)
{
//接口返回报文
string result = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
if (contentType == null)
{
request.ContentType = "application/x-www-form-urlencoded";
}
else
{
request.ContentType = contentType;
}
byte[] data = Encoding.GetEncoding("utf-8").GetBytes(postDataStr);
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
result = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
catch (WebException ex)
{
var errorSr = new StreamReader(ex.Response.GetResponseStream());
result = errorSr.ReadToEnd();
}
return result;
}
/// <summary>
/// Http的Get请求
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <returns></returns>
public static string GetHttpGet(string url, string postData)
{
//接口返回报文
string result = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + (postData == "" ? "" : "?") + postData);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
result = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
catch (WebException ex)
{
var errorSr = new StreamReader(ex.Response.GetResponseStream());
result = errorSr.ReadToEnd();
}
return result;
}
#endregion
#region 对应postMan 【Token】方式请求
/// <summary>
/// MultipartFormData方式请求(多表单)
/// </summary>
/// <param name="url"></param>
/// <param name="kVDatas"></param>
/// <param name="method"></param>
/// <param name="encoding"></param>
/// <param name="timeOut"></param>
/// <returns></returns>
public static string GetHttpPostByMultipartFormData(string url, NameValueCollection kVDatas, string method = WebRequestMethods.Http.Post, string encoding = "UTF-8", int timeOut = -1)
{
//接口返回报文
string result = string.Empty;
try
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = Encoding.GetEncoding(encoding).GetBytes("\r\n--" + boundary + "\r\n");
byte[] endbytes = Encoding.GetEncoding(encoding).GetBytes("\r\n--" + boundary + "--\r\n");
//1.HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = method;
request.KeepAlive = true;
request.Timeout = timeOut;
using (Stream stream = request.GetRequestStream())
{
//1.1 key/value
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
if (kVDatas != null)
{
foreach (string key in kVDatas.Keys)
{
stream.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, kVDatas[key]);
byte[] formitembytes = Encoding.GetEncoding(encoding).GetBytes(formitem);
stream.Write(formitembytes, 0, formitembytes.Length);
}
}
stream.Write(endbytes, 0, endbytes.Length);
}
//2.WebResponse
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
result = stream.ReadToEnd();
}
}
}
catch (WebException ex)
{
var errorSr = new StreamReader(ex.Response.GetResponseStream());
result = errorSr.ReadToEnd();
}
return result;
}
/// <summary>
/// Http的Post请求
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public static string GetHttpPostByHeaders(string Url, string postDataStr, Dictionary<string, string> dicList, string contentType = null)
{
//接口返回报文
string result = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
if (contentType == null)
{
request.ContentType = "application/x-www-form-urlencoded";
}
else
{
request.ContentType = contentType;
}
foreach (var dic in dicList)
{
request.Headers.Add(dic.Key, dic.Value);
}
byte[] data = Encoding.GetEncoding("utf-8").GetBytes(postDataStr);
request.ContentLength = data.Length;
Stream myRequestStream = request.GetRequestStream();
myRequestStream.Write(data, 0, data.Length);
myRequestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
result = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
catch (WebException ex)
{
var errorSr = new StreamReader(ex.Response.GetResponseStream());
result = errorSr.ReadToEnd();
}
return result;
}
#endregion
public static string GetHttpPostByToken(string url, string sendInfo, string token, string contentType = null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("token", token);
request.Method = "POST";
if (contentType == null)
{
request.ContentType = "application/json;charset=UTF-8";
}
else
{
request.ContentType = contentType;
}
byte[] data = Encoding.UTF8.GetBytes(sendInfo);
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
//接口返回
string result = string.Empty;
try
{
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));
result = reader.ReadToEnd();
}
catch (WebException ex)
{
var errorSr = new StreamReader(ex.Response.GetResponseStream());
result = errorSr.ReadToEnd();
}
return result;
}
}
}