我想创建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.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);