Logo

【C#】基于RestClient请求示例

photo

2024年02月18日

 RestSharp下载地址:

https://download.csdn.net/download/u012949335/87694650?spm=1001.2014.3001.5503

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Text;
using RestSharp;        
        /// <summary>
        /// RestClient请求(form-data)
        /// </summary>
        /// <param name="baseUrl">请求地址</param>
        /// <param name="headerParam">请求header</param>
        /// <param name="bodyData">请求body(form-data)</param>
        /// <param name="fileTypeName">form-data type=file的name命名名称</param>
        /// <param name="fileStream">文件流</param>
        /// <param name="fileName">上传文件名称</param>
        /// <param name="method">请求方法</param>
        /// <returns></returns>
        public static string PostInfoData(string baseUrl, Dictionary<string, string> headerParam, Dictionary<string, object> bodyData, string fileTypeName, Action<Stream> fileStream, string fileName, Method method = Method.POST)
        {
            RestClient client = new RestClient(baseUrl);
            client.Timeout = -1;
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertificateValidate);
            ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)192 | (System.Net.SecurityProtocolType)768 | (System.Net.SecurityProtocolType)3072;
            RestRequest request = new RestRequest(method);
            request.AlwaysMultipartFormData = true;
            if (headerParam != null && headerParam.Count > 0)
                foreach (var key in headerParam.Keys)
                    request.AddHeader(key, headerParam[key]);
            if (bodyData != null && bodyData.Count > 0)
                foreach (var key in bodyData.Keys)
                    request.AddParameter(key, bodyData[key]);
            if (!string.IsNullOrEmpty(fileName) && fileStream != null)
                request.AddFile(fileTypeName, fileStream, fileName);
            var boundary = DateTime.Now.Ticks.ToString("X");
            var ContentType = "multipart/form-data;boundary=----" + boundary;
            request.AddHeader("Content-Type", ContentType);
            IRestResponse response = client.Execute(request);
            return response.Content.ToString();
        }
        /// <summary>
        /// RestClient请求(application/json)
        /// </summary>
        /// <param name="baseUrl">请求地址</param>
        /// <param name="headerParam">请求header</param>
        /// <param name="jsonData">json对象</param>
        /// <param name="method">请求方法</param>
        /// <returns></returns>
        public static string PostInfoData(string baseUrl, Dictionary<string, string> headerParam, string jsonData, Method method = Method.POST)
        {
            RestClient client = new RestClient(baseUrl);
            client.Timeout = -1;
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertificateValidate);
            ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)192 | (System.Net.SecurityProtocolType)768 | (System.Net.SecurityProtocolType)3072;
            RestRequest request = new RestRequest(method);
            if (headerParam != null && headerParam.Count > 0)
                foreach (var key in headerParam.Keys)
                    request.AddHeader(key, headerParam[key]);
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("application/json", jsonData, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            return response.Content;
        }
        /// <summary>
        /// RestClient请求(form-data)
        /// </summary>
        /// <param name="baseUrl">请求地址</param>
        /// <param name="headerParam">请求header</param>
        /// <param name="bodyData">请求body(form-data)</param>
        /// <param name="method">请求方法</param>
        /// <returns></returns>
        public static byte[] PostInfoData(string baseUrl, Dictionary<string, string> headerParam, Dictionary<string, object> bodyData, Method method = Method.POST)
        {
            RestClient client = new RestClient(baseUrl);
            client.Timeout = -1;
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertificateValidate);
            ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)192 | (System.Net.SecurityProtocolType)768 | (System.Net.SecurityProtocolType)3072;
            RestRequest request = new RestRequest(method);
            request.AlwaysMultipartFormData = true;
            if (headerParam != null && headerParam.Count > 0)
                foreach (var key in headerParam.Keys)
                    request.AddHeader(key, headerParam[key]);
            if (bodyData != null && bodyData.Count > 0)
                foreach (var key in bodyData.Keys)
                    request.AddParameter(key, bodyData[key]);
            var boundary = DateTime.Now.Ticks.ToString("X");
            var ContentType = "multipart/form-data;boundary=----" + boundary;
            request.AddHeader("Content-Type", ContentType);
            IRestResponse response = client.Execute(request);
            return response.RawBytes;
        }

调用示例

1、RestClient请求(form-data)

public static string GetFile(Stream stream, string title, string fileType)
        {
            Dictionary<string, string> headerParam = new Dictionary<string, string>();
            headerParam.Add("x-xx", "s");
            headerParam.Add("x-xxx", "ss");
            headerParam.Add("x-xxxx", "sss");
            Dictionary<string, object> bodyData = new Dictionary<string, object>();
            bodyData.Add("title", title);
            bodyData.Add("fileType", fileType);
            var url = "xxxxxxx";
            var redata = PostInfoData(url, headerParam, bodyData, "file", stream.CopyTo, title + "." + fileType);
            return redata;
        }

2、RestClient请求(application/json)

public static string GetCreateInfo() 
        {
            Dictionary<string, string> headerParam = new Dictionary<string, string>();
            headerParam.Add("x-x", "s");
            headerParam.Add("x-xx", "ss");
            headerParam.Add("x-xxx", "sss");
            string jsondata =  "{\"conditions\":{ \"requestname\":\"" + "" + "\",\"workflowIds\":\"xx\"},\"pageNo\":\"" + 1 + "\",\"pageSize\":\"" + 1000 + "\",\"workId\":\"xx\",\"appId\":\"ssss\"}";
            var url = "sssssssss";
            var redata = PostInfoData(url, headerParam, jsondata);
            return redata;
        }

3、RestClient请求(form-data),返回byte[]

public static byte[] GetDownloadfile(string documentxx)
        {
            Dictionary<string, string> headerParam = new Dictionary<string, string>();
            headerParam.Add("x-x", "s");
            headerParam.Add("x-xx", "ss");
            headerParam.Add("x-xxx", "sss");
            Dictionary<string, object> bodyData = new Dictionary<string, object>();
            bodyData.Add("documentxx", documentxx);
            bodyData.Add("connet", "xxxxxx");
            var url = "sssssssss";
            var redbyte = PostInfoData(url, headerParam, bodyData, RestSharp.Method.GET);
            return redbyte;
        }

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!
Gitlab 定时备份 2024年02月05日

要求1.为了能够备份和恢复,请确保你的系统上安装了Rsync#Debian/Ubauntusu...Gitlab定时备份

热门文章

WordPress 后台编辑器样式实现直接预览 在WordPress3.0以后,有一个新的实用功能:你可以更改默认后台编辑器(TinyMCE)的样...WordPress后台编辑器样式实现直接预览 作者:Pastore Antonio
1451 浏览量
【干货】Chrome插件(扩展)开发全攻略 写在前面我花了将近一个多月的时间断断续续写下这篇博文,并精心写下完整demo,写博客的辛苦大家懂的...【干货】Chrome插件(扩展)开发全攻略 作者:Pastore Antonio
1417 浏览量
CentOS 编译错误+配置错误解决方法集合 ERROR:theHTTPXSLTmodulerequiresthelibxml2/l...CentOS编译错误+配置错误解决方法集合 作者:Pastore Antonio
1409 浏览量
WordPress中加载JavaScript脚本的方法 在WordPress中加载脚本(为CSS和JS,下同)文件,大多数人的做法是直接在hea...WordPress中加载JavaScript脚本的方法 作者:Pastore Antonio
1386 浏览量
wordpress学习五: 通过wordpress_xmlrpc的python包远程操作wordpress wordpress提供了丰富的xmlrpc接口api来供我们远程操控wp的内容。伟大的开源社区有人就...wordpress学习五:通过wordpress_xmlrpc的python包远程操作wordpress 作者:Pastore Antonio
1384 浏览量