我使用 RestSharp 作为底层 HTTP 客户端库,在黑盒服务上制作压力/吞吐量测试客户端。线程池和服务点连接限制已提高到 5000,但这应该不用担心,因为我们每秒测试大约 500-1000 个请求。高分辨率(微秒)计时器组件用于以我们想要测试的速率抛出请求。
RestSharp 代码大致如下
restClient.ExecuteAsync(postRequest, res =>
{
stopwatch.Stop();
lock (this.countLocker)
{
this.roundTrips.Add(stopwatch.ElapsedMilliseconds);
if (res.ResponseStatus == ResponseStatus.Completed &&
(res.StatusCode == HttpStatusCode.OK ||
res.StatusCode == HttpStatusCode.NoContent))
{
this.responseCount++;
}
else
{
// Treat all other status codes as errors.
this.reportError(res);
}
}
});
Run Code Online (Sandbox Code Playgroud)
在发送过多请求时,我们会观察到服务会在一段时间后溢出一些错误 503 响应,但 RestSharp 将这些响应视为完整响应,因为这是来自服务器的有效响应;没有抛出实际的异常。
不清楚的是,当 RestSharp 由于底层连接错误而遇到异常时
The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
at RestSharp.Http.GetRawResponseAsync(IAsyncResult result, Action`1 callback) …Run Code Online (Sandbox Code Playgroud) 这是 Postman 为成功调用我的页面而提供的(修改后的)片段。
var client = new RestClient("http://sub.example.com/wp-json/wp/v2/users/me");
var request = new RestRequest(Method.GET);
request.AddHeader("authorization", "Basic anVyYTp3MmZacmo2eGtBOHJsRWrt");
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
但是当放置在我的 c# 应用程序中时,它返回 403 forbidden,而 Postman 生成它并收到 200。当我在我的应用程序中使用 httpclient 时会发生同样的事情(403)。
我有一个 RestSharp 客户端和 Nancy Self Host Server。我想要的是
从客户端发送多部分表单数据并从服务器轻松解析该数据:
从 RestSharp 客户端发送二进制文件和 Json 数据作为多部分表单数据,并能够从 Nancy 服务器获取二进制文件和 Json 对象
在使用 Restsharp 的客户端:[ http://restsharp.org/ ] 我尝试发送“multipart/form-data”请求,其中包含一个二进制文件和一些 json 格式的元数据:
var client = new RestClient();
...
IRestRequest restRequest = new RestRequest("AcmeUrl", Method.POST);
restRequest.AlwaysMultipartFormData = true;
restRequest.RequestFormat = DataFormat.Json;
// I just add File To Request
restRequest.AddFile("AudioData", File.ReadAllBytes("filePath"), "AudioData");
// Then Add Json Object
MyObject myObject = new MyObject();
myObject.Attribute ="SomeAttribute";
....
restRequest.AddBody(myObject);
client.Execute<MyResponse>(request);
Run Code Online (Sandbox Code Playgroud)
在使用 Nancy[ http://nancyfx.org/ ] 的服务器上,尝试获取文件和 Json 对象 [元数据]
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试反序列化从 RestSharp 调用 API 返回的 JSON。
这是一个 C# 控制台应用程序。到目前为止,这是我的代码:
using System;
using RestSharp;
using RestSharp.Authenticators;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using RestSharp.Deserializers;
using System.Collections.Generic;
namespace TwilioTest
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient("https://api.twilio.com/2010-04-01");
var request = new RestRequest("Accounts/{{Account Sid}}/Messages.json", Method.GET);
request.AddParameter("To", "{{phone number}}");
request.AddParameter("From", "{{phone number}}");
client.Authenticator = new HttpBasicAuthenticator("{{account sid}}", "{{auth token}}");
var response = client.Execute(request);
var jsonResponse = JsonConvert.DeserializeObject(response.Content);
Console.WriteLine(jsonResponse);
}
}
}
Run Code Online (Sandbox Code Playgroud)
响应是 JSON 格式的消息列表,键包括"to"、"from"、"body"和 …
首先:我知道标题中问题的直接解决方案。
我知道每当在针对不同框架构建的项目下引用 dll 时,就会出现此问题。
我有一个针对 .NET Framework 4.0 构建的项目,我引用了针对相同框架的 RestSharp dll。
输出窗口中显示的错误是
The primary reference "RestSharp" could not be resolved because it was built against the ".NETFramework,Version=v4.6" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.0".
Run Code Online (Sandbox Code Playgroud)
我正在使用 VS2010。
当我阅读最新 RestSharp 的 readme.txt 时:
*** IMPORTANT CHANGE IN RESTSHARP VERSION 103 ***
In 103.0, JSON.NET was removed as a dependency.
If this is still installed in your project and no other libraries depend on
it you may remove it from your installed packages.
There is one breaking change: the default Json*Serializer* is no longer
compatible with Json.NET. To use Json.NET for serialization, copy the code
from https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279154f41a17b36f7/RestSharp/Serializers/JsonSerializer.cs
and register it with your client:
var client = new RestClient();
client.JsonSerializer …Run Code Online (Sandbox Code Playgroud) 我想使用 RestSharp 计算时间响应,但实际上我正在使用秒表功能来执行我的目标。是否有其他方法使用 restSharp 函数来获得时间响应?或者这是最好的选择?
public class LoadingTimes
{
public Stopwatch Stopwatch = new Stopwatch();
public List<double> GetResponsesTimesForSelectedPage(PageInformation pageInformation)
{
var responsesTimesList = new List<double>();
var client = new RestClient(pageInformation.Url);
var request = new RestRequest("/City/berlijn/", Method.GET);
for (int actualExecution = 1; actualExecution <= pageInformation.ExecutionsCount; actualExecution++)
{
Stopwatch.Start();
client.Execute(request);
Stopwatch.Stop();
responsesTimesList.Add(Stopwatch.Elapsed.TotalMilliseconds);
Stopwatch.Reset();
}
return responsesTimesList;
}
}
Run Code Online (Sandbox Code Playgroud) 我使用 RestSharp 从某个页面获取 json。回应是这样的:
{
"cars": [
{
"name": "car1",
"size": 10,
"color": "black"
},
{
"name": "car2",
"size": 20,
"color": "white"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我有一辆车的这个类:
public class Car
{
public string Name { get; set; }
public int Size { get; set; }
public string Color { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何将此响应映射到汽车列表?
如果没有"cars":[在 json 中,这很容易,但现在我需要这样做:
private class Cars
{
public List<Car> Cars { get; set; }
}
...
IRestResponse<Cars> response = client.Execute<Cars>(request);
...
...response.Data.Cars...
Run Code Online (Sandbox Code Playgroud)
但我觉得 Cars 类没用,我想做这样的事情: …
我正在编写一个搜索 API,它使用 async-await 和 awaiting Task.WhenAll 聚合来自许多微服务 API 的结果。我最多对 Search API 的每个请求进行 11 次微服务 API 调用。但是,我看到性能不一致 - 有时 Search API 需要 400 毫秒(可接受),而其他的则需要 1 秒以上(不可接受),即使我在本地运行/无负载时也是如此。大部分放缓似乎发生在 await Task.WhenAll 周围。
三个观察:
搜索 API 有时会启动对微服务 API 的 1/3 或 1/2 调用,然后等待 600-1000 毫秒以启动下一组调用。这种批处理行为是不一致的。
搜索 API 有时会调用每个微服务 API,获取结果并再等待 500-1000 毫秒。例如,Search API 可能需要 1000 毫秒,所有微服务请求都在 100 毫秒内开始,并在 400 毫秒或更短的时间内在内部完成。但是,在响应全部发回后,搜索 API 只会继续在 WhenAll 上停留 500 毫秒。
这种行为是不一致的。如果我运行 30 次,可能其中 5 或 10 次相对于其他 25 或 20 次需要超长的时间。
所有 API 都是用 ASP.NET Core MVC 编写的,在单个 docker 网络上的 …
我无法使用 Azure DevOps REST API 创建工作项,如中所述 工作项 - 创建中创建工作项
要求:
https://dev.azure.com/{organization}/MyTestProject/_apis/wit/workitems/$Task?api-version=6.0-preview.3
Request Body:
[
{
"op": "add",
"path": "/fields/System.Title",
"value": "Task2"
}
]
Run Code Online (Sandbox Code Playgroud)
获取响应的代码(注意此代码适用于所有其他 POST 请求):
using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
{
response.EnsureSuccessStatusCode();
JsonResponse = await response.Content.ReadAsStringAsync();
}
Response: 400
Run Code Online (Sandbox Code Playgroud)
有人可以建议吗?
restsharp ×10
c# ×7
json ×2
rest ×2
.net ×1
async-await ×1
azure-devops ×1
docker ×1
httpclient ×1
keep-alive ×1
nancy ×1
throughput ×1
wp-api ×1