之前在做C#调用Restful接口的时候,因为时间比较匆忙,一直没有把这个类好好整理一下。最近一方面对于ContentType的整理感觉很不方便,另一方面对于Restful接口http和https调用使用不同的类感觉很不方便,发现调用http和https只要用一句话就可以解决了。于是,整理一个通用类,记录下来,方便以后使用。
感谢网上各路大神提供的资料。
代码如下:
using System;
using System.IO;
using System.Net;
using System.Text;
// 添加https
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
// end添加https
namespace NT_MiddleDataService.ADO
{
public enum HttpVerbNew
{
GET, //method 常用的就这几样,可以添加其他的 get:获取 post:修改 put:写入 delete:删除
POST,
PUT,
DELETE
}
public class ContentType//根据Postman整理,可以添加
{
public string Text = "text/plain";
public string JSON = "application/json";
public string Javascript = "application/javascript";
public string XML = "application/xml";
public string TextXML = "text/xml";
public string HTML = "text/html";
}
public class RestApiClient
{
public string EndPoint { get; set; } //请求的url地址
public HttpVerbNew Method { get; set; } //请求的方法
public string ContentType { get; set; } //格式类型
public string PostData { get; set; } //传送的数据
public RestApiClient()
{
EndPoint = "";
Method = HttpVerbNew.GET;
ContentType = "text/xml";
PostData = "";
}
public RestApiClient(string endpoint, string contentType)
{
EndPoint = endpoint;
Method = HttpVerbNew.GET;
ContentType = contentType;
PostData = "";
}
public RestApiClient(string endpoint, HttpVerbNew method, string contentType)
{
EndPoint = endpoint;
Method = method;
ContentType = contentType;
PostData = "";
}
public RestApiClient(string endpoint, HttpVerbNew method, string contentType, string postData)
{
EndPoint = endpoint;
Method = method;
ContentType = contentType;
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
public string MakeRequest()
{
return MakeRequest("");
}
public string MakeRequest(string parameters)
{
var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
// 添加https
if (EndPoint.Substring(0, 8) == "https://")
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
}
// end添加https
request.Method = Method.ToString();
request.ContentLength = 0;
request.ContentType = ContentType;
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerbNew.POST)//如果传送的数据不为空,并且方法是post
{
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);//编码方式按自己需求进行更改,我在项目中使用的是UTF-8
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerbNew.PUT)//如果传送的数据不为空,并且方法是put
{
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").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;
}
}
public bool CheckUrl(string parameters)
{
bool bResult = true;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
myRequest.Method = Method.ToString(); //设置提交方式可以为"get","head"等
myRequest.Timeout = 10000; //设置网页响应时间长度
myRequest.AllowAutoRedirect = false;//是否允许自动重定向
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
bResult = (myResponse.StatusCode == HttpStatusCode.OK);//返回响应的状态
return bResult;
}
}
}
知行办公,专业移动办公平台 https://zx.naton.cn/
【总监】十二春秋之,3483099@qq.com;
【Master】zelo,616701261@qq.com;
【运营】运维艄公,897221533@qq.com;
【产品设计】流浪猫,364994559@qq.com;
【体验设计】兜兜,2435632247@qq.com;
【iOS】淘码小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;
【Android】人猿居士,1059604515@qq.com;思路的顿悟,1217022114@qq.com;
【java】首席工程师MR_W,feixue300@qq.com;
【测试】土镜问道,847071279@qq.com;
【数据】fox009521,42151960@qq.com;
【安全】保密,你懂的。