using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace Test.NetCore.Communication
{
public enum HttpVerb
{
GET, //method 常用的就这几样,可以添加其他的 get:获取 post:修改 put:写入 delete:删除
POST,
PUT,
DELETE
}
public class ContentType//根据Postman整理,可以添加
{
public static string Text = "text/plain";
public static string JSON = "application/json";
public static string Javascript = "application/javascript";
public static string XML = "application/xml";
public static string TextXML = "text/xml";
public static string HTML = "text/html";
}
public class RestClient
{
public string EndPoint { get; set; } //请求的url地址
public HttpVerb Method { get; set; } //请求的方法
public string ContentType { get; set; } //格式类型
public string PostData { get; set; } //传送的数据
public Dictionary<string, string> HeadersList { get; set; }
public RestClient()
{
EndPoint = "";
Method = HttpVerb.GET;
ContentType = "text/xml";
PostData = "";
}
public RestClient(string endpoint, string contentType)
{
EndPoint = endpoint;
Method = HttpVerb.GET;
ContentType = contentType;
PostData = "";
}
public RestClient(string endpoint, string contentType, Dictionary<string, string> headerslist)
{
EndPoint = endpoint;
Method = HttpVerb.GET;
ContentType = contentType;
HeadersList = headerslist;
PostData = "";
}
public RestClient(string endpoint, string contentType, Dictionary<string, string> headerslist, HttpVerb method)
{
EndPoint = endpoint;
Method = method;
ContentType = contentType;
HeadersList = headerslist;
PostData = "";
}
public RestClient(string endpoint, string contentType, Dictionary<string, string> headerslist, HttpVerb method, string postData)
{
EndPoint = endpoint;
Method = method;
ContentType = contentType;
HeadersList = headerslist;
PostData = postData;
}
// 添加https
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
// end添加https
/// <summary>
/// 开始请求
/// </summary>
/// <returns></returns>
public string MakeRequest()
{
return MakeRequest("");
}
public string MakeRequest(string parameters)
{
// 添加https
if (EndPoint.Substring(0, 8) == "https://")
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
ServicePointManager.DefaultConnectionLimit = 9999;
// request.UserAgent = DefaultUserAgent;
}
var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
// 添加https
if (EndPoint.Substring(0, 8) == "https://")
{
request.UserAgent = DefaultUserAgent;
}
// end添加https
request.Method = Method.ToString();
request.ContentLength = 0;
request.ContentType = ContentType;
if (HeadersList != null && HeadersList.Count > 0)
{
foreach (var p in HeadersList)
{
request.Headers.Add(p.Key, p.Value);
}
}
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)//如果传送的数据不为空,并且方法是post
{
var encoding = new UTF8Encoding();
var bytes = Encoding.UTF8.GetBytes(PostData);//编码方式按自己需求进行更改,我在项目中使用的是UTF-8
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.PUT)//如果传送的数据不为空,并且方法是put
{
var encoding = new UTF8Encoding();
var bytes = Encoding.UTF8.GetBytes(PostData);//编码方式按自己需求进行更改,我在项目中使用的是UTF-8
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
// grab the response
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
} // class
}