是否有任何REST(Http)客户端库可用于.net core 2,如RestSharp ..?我一直在使用RestSharp,但它不支持.net core 2所以寻找一个。如果您有任何建议,请告诉我...在此先感谢..
是的。尝试:https://flurl.dev/
// Flurl will use 1 HttpClient instance per host
var person = await "https://api.com"
.AppendPathSegment("person")
.SetQueryParams(new { a = 1, b = 2 })
.WithOAuthBearerToken("my_oauth_token")
.PostJsonAsync(new
{
first_name = "Claire",
last_name = "Underwood"
})
.ReceiveJson<Person>();
或者使用WebRequest
库创建自己的REST客户端,如下所示:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
public class HTTPService
{
private readonly CookieContainer _cc;
private readonly string _jsonContentType;
public HTTPService()
{
_cc = new CookieContainer();
_jsonContentType = "application/json";
}
public async Task<string> Get(string url)
{
var request = WebRequest.CreateHttp(url);
request.CookieContainer = _cc;
request.Method = nameof(Get).ToUpper();
request.ContentType = _jsonContentType;
return await HTTPMethods.ReadFromResponseAsync(request);
}
public async Task<string> Post(string url, string postDataStr)
{
var request = WebRequest.CreateHttp(url);
request.CookieContainer = _cc;
request.Method = nameof(Post).ToUpper();
request.ContentType = _jsonContentType;
await HTTPMethods.SendRequestAsync(request, postDataStr.ToString().TrimStart('?'));
return await HTTPMethods.ReadFromResponseAsync(request);
}
}
public static class HTTPMethods
{
public static async Task SendRequestAsync(HttpWebRequest request, string message)
{
using (var myRequestStream = await request.GetRequestStreamAsync())
{
using (var myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8")))
{
await myStreamWriter.WriteAsync(message);
myStreamWriter.Close();
}
myRequestStream.Close();
}
}
public static async Task<string> ReadFromResponseAsync(HttpWebRequest request)
{
var retString = string.Empty;
using (var response = await request.GetResponseAsync())
{
using (var myResponseStream = response.GetResponseStream())
{
using (var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")))
{
retString = await myStreamReader.ReadToEndAsync();
myStreamReader.Close();
}
myResponseStream.Close();
}
response.Close();
}
return retString;
}
}
你可以尝试IEnterprise.Easy-HTTP,因为它内置了类解析和查询构建:
await new RequestBuilder<ExampleObject>()
.SetHost("https://httpbin.org")
.SetContentType(ContentType.Application_Json)
.SetType(RequestType.Post)
.SetModelToSerialize(dto)
.Build()
.Execute();
我是图书馆的作者,所以随时提问或查看github中的代码:)
目前它只支持json和form-data,但我正在努力添加xml解析。
是的。
rest client.net
https://github.com/MelbourneDeveloper/RestClient.Net
NuGet:RestClient.Net
这是我的图书馆,你可以打击我的回购支持。在Android,UWP和.NET Core / Framework上进行了大量测试。
我从2015年起就维护了DalSoft.RestClient它适用于.NET Standard 2.0,这意味着它适用于全胖.NET和所有.NET Core版本 - 包括新的.NET Core 2.1功能,如HttpClientFactory。
DalSoft.RestClient简单但非常易于扩展和灵活,它被大型企业积极维护和战斗测试,每天都会产生数百万的请求。
https://restclient.dalsoft.io/
这是多么容易,这将获得我所有的公共GitHub回购:
dynamic restClient = new RestClient("https://api.github.com");
var repositories = await restClient
.Headers(new { UserAgent = "MyClient" } // Github requires User-Agent to be set
.users.dalsoft.repos.Get();
foreach (var repo in repositories)
{
Console.WriteLine(repo.name);
}