我正在调用一个web服务来通过post方法发送json数据。这是代码:
string sample_url = "mywebservice/createsample;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sample_url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = new CookieContainer();
CookieCollection cookcol = new CookieCollection();
cookcol.Add(cookie);
string body= json data
byte[] postBytes = Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Close();
WebResponse response = request.GetResponse();
string satus = ((HttpWebResponse)response).StatusDescription;
postStream = response.GetResponseStream();
StreamReader reader = new StreamReader(postStream);
我需要获取一些关于此Web服务请求和响应的时间戳,因为服务似乎很慢。如何将倒数计时器放在这个位置?感谢您的答复。
答案 0 :(得分:2)
您可以使用Stopwatch类:
Stopwatch sw = new Stopwatch();
sw.Start();
// your code to measure here
sw.Stop();
// print sw.Elapsed
答案 1 :(得分:0)
请注意,您也没有测试自己的网络延迟。如果您特别感兴趣的是每次服务调用需要多长时间,那么根据@nvoigt的答案,您可能应该使用Stopwatch
,但是请将此部分用于您的服务实现方法。