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定时备份

热门文章

Navicat Premium 12.0.22 安装与破解 一、安装  NavicatPremium12.0.22的下载链接:https://pan.ba...NavicatPremium12.0.22安装与破解 作者:Pastore Antonio
1515 浏览量
C# JArray与JObject 的使用 STEP1、usingNewtonsoft.Json.Linq;STEP2、如何获取json里的...C#JArray与JObject的使用 作者:Pastore Antonio
1465 浏览量
解决mysql的配置ONLY_FULL_GROUP_BY引起的错误 由于自己的本地网站环境使用了mysql8.0的版本,在测试一个groupby的sql查...解决mysql的配置ONLY_FULL_GROUP_BY引起的错误 作者:Pastore Antonio
1448 浏览量
Windows server 2016 英文服务器安装中文语言包教程 1、下载windowsserver2016语言包下载地址:https://pan.baidu....Windowsserver2016英文服务器安装中文语言包教程 作者:Pastore Antonio
1406 浏览量
assembly(全局程序集缓存)中dll的导入和导出 本文主要讲一下关于assembly中的dll是怎么导入和导出的。浏览量:1,468 作者:Pastore Antonio
1403 浏览量