获取异步请求的经过时间
我试图获取异步请求期间经过的时间,但我得到的时间与wireshark时间完全不同。
这真的很奇怪,因为我正在对同步请求池做同样的事情,并且我得到了wireshark时间。
这是我的代码:
public void getResponseAsync()
{
RequestState rs = new RequestState();
rs.Request = this.webRequest; //On ajoute la requete dans l'objet état pour pouvoir le récupérer dans la callback
this.timerAsync.Restart();
IAsyncResult ar = rs.Request.BeginGetResponse(new AsyncCallback(this.ResponseCallback), rs); // Appel asynchrone
}
public void ResponseCallback(IAsyncResult ar)
{
RequestState rs = (RequestState)ar.AsyncState; //Récupération de l'objet etat
HttpWebRequest req = rs.Request; //Récupération de la requete web (object HttpWebRequest)
try //Récupération de la réponse Web
{
HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar);
this.timerAsync.Stop();
this.timeAsync = this.timerAsync.ElapsedMilliseconds;
...
...
}
}
this.timeAsync 的值与 Wireshark 值相差甚远。
我做错了什么吗?
感谢您的帮助。
I'm trying to get the elapsed time during an asynchronous request, but I got a time which is totally different from the wireshark time.
That's really weird because i'm doing the same thing with a pool of synchronous requests and I got the wireshark time.
That is my code :
public void getResponseAsync()
{
RequestState rs = new RequestState();
rs.Request = this.webRequest; //On ajoute la requete dans l'objet état pour pouvoir le récupérer dans la callback
this.timerAsync.Restart();
IAsyncResult ar = rs.Request.BeginGetResponse(new AsyncCallback(this.ResponseCallback), rs); // Appel asynchrone
}
public void ResponseCallback(IAsyncResult ar)
{
RequestState rs = (RequestState)ar.AsyncState; //Récupération de l'objet etat
HttpWebRequest req = rs.Request; //Récupération de la requete web (object HttpWebRequest)
try //Récupération de la réponse Web
{
HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar);
this.timerAsync.Stop();
this.timeAsync = this.timerAsync.ElapsedMilliseconds;
...
...
}
}
The value of this.timeAsync is far away from the Wireshark value.
Did I make something wrong?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定只有一次 getResponseAsync() 调用吗?
在 getResponseAsync 上添加断点以确保确定。
多次调用 getResponseAsync 将重置您的计时器。
Are you sure that there is only one call of getResponseAsync()?
Add a breakpoint on getResponseAsync to be certain.
Multiple calls of getResponseAsync will reset your timer.