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

热门文章

修复群晖Synology Drive client右键菜单缺失问题 本教程主要解决windows10右键菜单中没有SynologyDrive菜单的问题,整体思路是找到...修复群晖SynologyDriveclient右键菜单缺失问题 作者:Pastore Antonio
1827 浏览量
docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 前言:有时候我们会在docker上下载一个镜像,或者是上传一个镜像到docker上,甚至有时候就是在...docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 作者:Pastore Antonio
1808 浏览量
Adobe Acrobat Pro 激活 这里记录了一些AdobeAcrobat的激活教程和组件。浏览量:1,688 作者:Pastore Antonio
1535 浏览量
configure: error: Package requirements (oniguruma) were not met configure:error:Packagerequirements(oniguruma)...configure:error:Packagerequirements(oniguruma)werenotmet 作者:Pastore Antonio
1535 浏览量
追寻日出,找回自己 为什么我要去追寻日出?其实我是一个很懒的人,每次都起不来,直到有一次我在租房中睡到了大天亮,阳光照...追寻日出,找回自己 作者:Pastore Antonio
1515 浏览量