restsharp 并发请求_JAVA应用

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

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MT5和MT4交易软件有什么区别?

    MT5和MT4交易软件有什么区别?很多人说MT5从字面上看是MT4的升级版,但实际上MT5并不是MT4的升级版,各是各。(这是他们官方说的不是升级版。)MT4与MT5的系统区别1、最大的区别在于,MT5无锁仓功能,而MT4有锁仓功能2、MT4和MT5的指标脚本EA,依然兼容性很差,可以理解为2个独立的东西,只是语法上有很多类似的3、MT5增加了更多的周期,满足更多不同的需要4、历史数据加载不同:MT4采用hst,每个周期…

    2022年5月7日
    141
  • Servlet和JSP的交互方式[通俗易懂]

    Servlet和JSP的交互方式[通俗易懂]文章目录Servlet和JSP的交互方式Servlet:jsp:1.表单:2.超链接超链接3.forward:获取参数值request.getParameter(“id”)处理乱码newString(request.getParameter(“name”).getBytes(“ISO8859_1″),”UTF-8”)Servlet和JSP的交互方式Servlet:1.request.getRequestDispatcher(“url”).forward(requsest,response);这个是

    2022年6月22日
    22
  • anaconda与pycharm配合使用_python环境搭建

    anaconda与pycharm配合使用_python环境搭建前天下了一个GitHub开源项目,但是项目的运行是可以用终端命令行运行,以前没用过,在网上也没找到教程说明。下面我来说个简单教程。这个是搭配anaconda的配置环境使用的。首先点pycharm的terminal,就会进入终端命令行,进去了一般是进入了cmd命令行界面,这个时候的环境是你系统自带的python环境,想要配合使用anaconda还要进一步配置点windows找到上面那个嘿嘿的Anacondaprompt,右键进入文件夹,再右键点击属性,进入下面的界面将包括cm

    2022年8月28日
    2
  • css3动画特效_css3动画效果大全

    css3动画特效_css3动画效果大全CSS3为我们带来了令人惊叹的新特性,而最有趣的就是CSS动画。今天彬Go向大家推荐这50个CSS动画集合可以让你通过使用JavaScript函数来让动画更生动。为了能够预览到这些惊人的CSS3技术带

    2022年8月2日
    5
  • trunk链路的配置命令_链路聚合配置

    trunk链路的配置命令_链路聚合配置实验环境使用之前用过的GNS3、WireShark和CRT进入GNS3页面,点击左侧图标菜单栏,拖入2台路由器到拓扑操作台,再点击PC图标,选择VPC,拖入拓扑操作台,配置两台路由器(更改名称、更改图标、添加二层业务单板、添加磁盘容量为128Mb)—————-配置环境————-1、接下来用网线把PC和路由器连接起来将PC1连到SW1的f1/0将PC2连…

    2022年10月29日
    0
  • allure 报告[通俗易懂]

    allure 报告[通俗易懂]一、简介二、下载安装三、报告生成四、环境配置五、Python使用allure方法一、简介官方文档:https://docs.qameta.io/allure/二、下载安装1、linux下载安装先检查是否安装npm:whichnpm 未安装npm的话:curl–silent–locationhttps://rpm.nodesource.com/setup_10.x|bash- 安装:yuminstall-ynodejs …

    2022年7月26日
    19

发表回复

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

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