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


相关推荐

  • Python gzip和bz2模块 读写.gz 和.bz2压缩文件

    Python gzip和bz2模块 读写.gz 和.bz2压缩文件读写压缩文件问题你想读写一个gzip或bz2格式的压缩文件。解决方案gzip和bz2模块可以很容易的处理这些文件。两个模块都为open()函数提供了另外的实现来解决这个问题。比如,为了以文本形式读取压缩文件,可以这样做:#gzipcompressionimportgzipwithgzip.open(‘somefile.gz’,’rt’)asf:…

    2022年5月23日
    35
  • Ubuntu安装Opencv3

    Ubuntu安装Opencv3Ubuntu 安装 Opencv31 安装准备 2 安装 3 配置环境 4 检验环境 Ubuntu20 04 Opencv3 4 141 安装准备 1 1 安装 cmakesudoapt getinstallcm 2 安装依赖环境 sudoapt getinstallbu essentiallib 0 devlibavcode devlibavform devlibjpeg devlibswscal devlibtiff5 devsudoapt

    2025年9月1日
    0
  • kernel logo到开机动画之间闪现黑屏(android 5.X)

    kernel logo到开机动画之间闪现黑屏(android 5.X)

    2022年2月6日
    55
  • mediumtext_mysql数据类型介绍(含text,longtext,mediumtext说明) | 学步园[通俗易懂]

    mediumtext_mysql数据类型介绍(含text,longtext,mediumtext说明) | 学步园[通俗易懂]由MySQL支持的列类型列在下面。下列代码字母用于描述中:M指出最大的显示尺寸。最大的合法的显示尺寸是255。D适用于浮点类型并且指出跟随在十进制小数点后的数码的数量。最大可能的值是30,但是应该不大于M-2。方括号(“[”和“]”)指出可选的类型修饰符的部分。注意,如果你指定一个了为ZEROFILL,MySQL将为该列自动地增加UNSIGNED属性。TINYINT[(M)][UNSIG…

    2022年5月5日
    601
  • 【原创】互联网音视频直播架构方案(技术点)

    【原创】互联网音视频直播架构方案(技术点) 

    2022年7月4日
    23
  • 什么叫侧面指纹识别_屏下指纹和侧面指纹触控有什么区别

    什么叫侧面指纹识别_屏下指纹和侧面指纹触控有什么区别指纹识别作为一种生物识别方案,在手机上的应用为用户日常使用带来了极大的便利,从解锁手机到应用加密再到支付等场景,原本需要输密码的繁琐场景如今按一下手指就行。随着手机的发展,指纹解锁也出现了不同的解决方案,比如OPPOK3所采用的目前主流的屏下指纹解锁,以及荣耀9X所采用侧面指纹解锁。那么在实际应用场景中,哪种指纹识别更实用呢?采用了屏幕指纹的OPPOK3与侧面指纹的荣耀9X都有着真全面屏的设计…

    2022年6月30日
    43

发表回复

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

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