restsharp 并发请求_JAVA应用

restsharp 并发请求_JAVA应用C#RestSharp应用开通博客是想将自己所学的东西记录下来,以便自己查缺补漏,希望自己能坚持下去正题关于RestSharp的使用下载NuGet直接搜索即可,最新版本RestSharp需要.netframework4.5.2及以上支持Json序列化工具:Newtonsoft.Json,直接由NuGet下载官网说明:varclient=newRestClient(“http://ex…


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

C# RestSharp应用

开通博客是想将自己所学的东西记录下来,以便自己查缺补漏,希望自己能坚持下去

正题关于RestSharp的使用

下载

NuGet直接搜索即可,最新版本RestSharp需要.net framework 4.5.2及以上支持

Json序列化工具:Newtonsoft.Json,直接由NuGet下载

官网说明:

var client = new RestClient(“http://example.com”);

// client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest(“resource/{id}”);

request.AddParameter(“name”, “value”); // adds to POST or URL querystring based on Method

request.AddUrlSegment(“id”, “123”); // replaces matching token in request.Resource

// add parameters for all properties on an object

request.AddJsonObject(@object);

// or just whitelisted properties

request.AddObject(object, “PersonId”, “Name”, …);

// easily add HTTP Headers

request.AddHeader(“header”, “value”);

// add files to upload (works with compatible verbs)

request.AddFile(“file”, path);

// execute the request

var response = client.Post(request);

var content = response.Content; // raw content as string

// or automatically deserialize result

// return content type is sniffed but can be explicitly set via RestClient.AddHandler();

var response2 = client.Post(request);

var name = response2.Data.Name;

// or download and save file to disk

client.DownloadData(request).SaveAs(path);

// easy async support

await client.ExecuteAsync(request);

// async with deserialization

var asyncHandle = client.PostAsync(request, response => {

Console.WriteLine(response.Data.Name);

});

// abort the request on demand

asyncHandle.Abort();

写一个工具类

public class RestSharpApiUtil

{

#region 暴露执行方法

///

/// 组装Client,Request,并执行Http请求

///

/// 返回值类型

/// 基地址

/// 相对地址

/// 请求类型

/// Get/Put/Delete/Post等参数

/// post请求体

///

public static ResponseMessage RestAction(string baseUrl, string relativeUrl, Method method=Method.GET,List lstParam=null)

{

var client = new RestClient(baseUrl);

return RestMethod(client, InstallRequest(relativeUrl,method,lstParam));

}

///

/// 异步请求无返回值

///

///

///

///

///

///

public static void RestActionNoResponseAsync(string baseUrl, string relativeUrl, Method method = Method.GET, List lstParam = null)

{

var client = new RestClient(baseUrl);

RestMethodWithOutReturnAsync(client, InstallRequest(relativeUrl, method, lstParam));

}

#endregion

#region 底层调用,并不暴露方法

///

/// Http请求

///

///

///

///

///

static ResponseMessage RestMethod(RestClient client, RestRequest request)

{

RestResponse restResponse = (RestResponse)client.Execute(request);

try

{

return restResponse == null ? new ResponseMessage() :

string.IsNullOrWhiteSpace(restResponse.Content) ? new ResponseMessage() :

JsonConvert.DeserializeObject>(restResponse.Content);

}

catch (Exception ex)

{

return new ResponseMessage() { success = false };

}

}

///

/// 无返回值异步调用

///

///

///

static void RestMethodWithOutReturnAsync(RestClient client, RestRequest request)

{

RestResponse restResponse;

var asyncHandle = client.ExecuteAsync(request, response =>

{

restResponse = (RestResponse)response;

});

asyncHandle.Abort();

}

///

/// 组装Request

///

///

///

///

///

static RestRequest InstallRequest(string relativeUrl, Method method = Method.GET, List lstParam = null)

{

var request = new RestRequest(relativeUrl, method);

if (lstParam != null)

{

foreach (RestParam p in lstParam)

{

switch (p.ParamType)

{

case EmParType.UrlSegment:

request.AddUrlSegment(p.Key, p.Value);

break;

case EmParType.Param:

request.AddParameter(p.Key, p.Value);

break;

case EmParType.Body:

request.AddJsonBody(p.Value);

break;

default:

break;

}

}

}

return request;

}

#endregion

}

其中用到两个类:ResponseMessage:返回值实体,根据自己的业务组装即可

RestParam:参数实体以及参数类型

转自:https://blog.csdn.net/dengzitui/article/details/88715385

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195876.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Nginx 和 Apache 区别最全详解?「建议收藏」

    Nginx 和 Apache 区别最全详解?「建议收藏」Nginx轻量级,采用C进行编写,同样的web服务,会占用更少的内存及资源抗并发,nginx以epollandkqueue作为开发模型,处理请求是异步非阻塞的,负载能力比apache高很多,而apache则是阻塞型的。在高并发下nginx能保持低资源低消耗高性能,而apache在PHP处理慢或者前端压力很大的情况下,很容易出现进程数飙升,从而拒绝服务的现象。nginx处理静态文件好,静态处理性能比apache高三倍以上nginx的设计高度模块化,编写模

    2022年5月2日
  • linux smartctl 命令,Linux下硬盘检测工具smartmontools(smartctl)使用方法

    linux smartctl 命令,Linux下硬盘检测工具smartmontools(smartctl)使用方法安装:yuminstallsmartmontoolshelp:#smartctl–helpsmartctlversion5.38[i686-redhat-linux-gnu]Copyright(C)2002-8BruceAllenHomepageishttp://smartmontools.sourceforge.net/Usage:smartctl[options…

    2022年10月8日
  • mysql导入excel表异常_mysql导入excel表格数据时出错的解决

    mysql导入excel表异常_mysql导入excel表格数据时出错的解决NavicatforMySQL导入数据时报错1:导入的是Excel2007表格格式的数据。2:报错以后数据加进去了。(选择了错误继续执行)3:这个错误对我的数据有影响吗?4:造成这个错误的原因是什么5:这个是日志文件[2012-07-1113:57:48][Msg]Importstart[2012-07-1113:57:48][Msg]Importtype-Excel20…

    2022年9月21日
  • Jenkins安装_jenkins sonar

    Jenkins安装_jenkins sonar前言jenkins的环境搭建方法有很多,本篇使用docker快速搭建一个jenkins环境。环境准备:mac/Linuxdockerdocker拉去jenkins镜像先下载jenkins镜

    2022年7月31日
  • 什么是跨域?跨域解决方法

    什么是跨域?跨域解决方法一、为什么会出现跨域问题出于浏览器的同源策略限制。同源策略(Sameoriginpolicy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互。所谓同源(即指在同一个域)就是两个页面具有相同的协…

    2022年4月28日
  • mysql workbench怎么导入数据库sql文件_workbench怎么创建数据库

    mysql workbench怎么导入数据库sql文件_workbench怎么创建数据库把Excel表格通过MySqlWorkbench导入数据库表中的使用总结今天接到一个任务,把excel表中的数据导入到mysql数据库中,通过半个多小时的鼓捣,基本上摸清了里面的门道。首先,准备所要导入的ecxel文件,需要把excel另存为*.csv类型的文件,然后通过notepad转换编码格式为utf-8类型,注意要把表头删去,否则会出问题。其次,导入所转换的文件

    2022年9月21日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号