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


相关推荐

  • opencv识别多条形码数字_opencv测试代码

    opencv识别多条形码数字_opencv测试代码这其实是一个小工程完成的功能: 使用摄像头采集图像进行预处理(检测部分) 提取出预处理的条形码图像(识别部分) 将条形码进行存入数据库(存储部分) 首先接到这个图像识别的小工程需要先确定这个工程的最初输入,和最后输出,输入就是普通的RGB图像,输出是数据库文件。其中需要完成的过程,就是我需要做得功能,检测部分、识别部分和存储部分,话不多说,上部分代码:/…

    2025年6月27日
    4
  • C++ – 容器(container)的erase()函数

    C++ – 容器(container)的erase()函数

    2021年12月2日
    56
  • oracle修改用户密码语句_oracle查询密码修改记录

    oracle修改用户密码语句_oracle查询密码修改记录GPS平台、网站建设、软件开发、系统运维,找森大网络科技!https://cnsendnet.taobao.com来自森大科技官方博客http://www.cnsendblog.com/index.php/?p=1596SYS用户是Oracle中权限最高的用户,而SYSTEM是一个用于数据库管理的用户。在数据库安装完之后,应立即修改SYS,SYSTEM这两个用户的密码,以保证数据库的安全。安装完之后修改密码方法cmd命令行下输入sqlplus/assysdba;法1.SQL>al

    2022年7月28日
    3
  • 精选国外免费PHP空间推荐

    精选国外免费PHP空间推荐精选国外免费PHP空间推荐方法/步骤000webhost–1500M支持PHP可绑米免费虚拟主机免费提供1500M空间,100G流量,FTP、Web方式上传管理文件,支持PHP5,提供2个M

    2022年7月2日
    27
  • clion 2021.4.2激活码破解方法

    clion 2021.4.2激活码破解方法,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月14日
    79
  • RHEL7/Centos7 i686/32bit glibc/stdc++ lib

    RHEL7/Centos7 i686/32bit glibc/stdc++ lib[hushui@HikvisionOSyum.repos.d]$sudoyuminstallglibc.i686Loadedplugins:fastestmirrorDeterminingfastestmirrorslocal|3.6kB00:00(1/2):local/group_gz…

    2022年6月5日
    44

发表回复

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

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