C# 调用http和https请求rest接口通用操作类
来源:互联网 发布:js获取当前window对象 编辑:程序博客网 时间:2024/06/05 20:53
之前在做C#调用Restful接口的时候,因为时间比较匆忙,一直没有把这个类好好整理一下。最近一方面对于ContentType的整理感觉很不方便,另一方面对于Restful接口http和https调用使用不同的类感觉很不方便,发现调用http和https只要用一句话就可以解决了。于是,整理一个通用类,记录下来,方便以后使用。
感谢网上各路大神提供的资料。
代码如下:
using System;using System.IO;using System.Net;using System.Text;// 添加httpsusing System.Net.Security;using System.Security.Cryptography.X509Certificates;// end添加httpsnamespace 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;
【安全】保密,你懂的。
阅读全文
0 0
- C# 调用http和https请求rest接口通用操作类
- http/https接口调用
- https和http协议接口post请求接口方法
- jdk 调用http https/请求
- C#处理和对接HTTP接口请求
- c#调用HTTP请求
- C#通过发送 http 请求调用接口之Get
- C#通过发送 http 请求调用接口之Post
- http请求接口,http接口远程调用
- C#调用HTTP接口
- C#调用HTTP接口
- C#调用HTTP接口
- 调用http接口操作
- Http 和 Https 访问请求
- 发起https和http请求
- android http 和https请求
- C#模拟Http与Https请求框架类实例
- C# 调用rest接口 cookie的保持
- first lambda表达式
- IOS自动化定位方式
- Dapper功能讲解
- SGISTL源码探究-默认使用的配置器
- git 添加文件和目录的时候,老是不能包括在git里。
- C# 调用http和https请求rest接口通用操作类
- Centos 7安装Apache之后,在浏览器中无法访问
- 【13】vue.js — 自定义指定(directive)
- linux根据时间判断删除多余旧的文件
- 【Angular】ng-repeat遍历输出数组
- Nginx高级配置之Gzip压缩配置
- 模拟实现strcmp函数
- 备战笔记
- 程序员面试技巧:如何通过索引说数据库优化能力