【问题标题】:process time response rest api处理时间响应休息api
【发布时间】:2016-05-20 09:56:01
【问题描述】:

我想创建 REST API 客户端,它可以处理我从服务器获得的任何响应的时间。

var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource

// easily add HTTP Headers
request.AddHeader("header", "value");

// add files to upload (works with compatible verbs)
request.AddFile(path);

// execute the request
IRestResponse response = client.Execute(request);

例如,我拿了示例代码,我在发送请求的同一行得到响应。

我应该运行另一个线程来计算时间,还是有其他方法?

【问题讨论】:

  • 您是否只是想测量调用此服务所需的时间?如果是这样,只需使用 System.Diagnostics.Stopwatch。
  • 我想计算从发送请求到收到响应的时间,可以吗?

标签: c# api rest time response


【解决方案1】:

如果您只想要一个代码块的经过时间,您可以通过使用 System.Diagnostic.Stopwatch 包装它来测量它。 ElapsedMilliseconds 为您提供 ms,或者您可以使用 Elapsed 属性取回 Timespan 对象。例如:

// Start a new stopwatch timer.
var timePerParse = Stopwatch.StartNew();

// code for operation you want to measure

timePerParse.Stop();
var elapsedMS = timePerParse.ElapsedMilliseconds;
Console.WriteLine(elapsedMS);

【讨论】:

    猜你喜欢
    • 2018-01-30
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2023-03-30
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多