当前位置: 首页 > 工具软件 > RESTClient > 使用案例 >

【C#】基于RestClient请求示例

储阳曦
2023-12-01

 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;
        }

 类似资料: