获取异步请求的经过时间

发布于 2024-12-15 22:12:10 字数 1141 浏览 3 评论 0原文

我试图获取异步请求期间经过的时间,但我得到的时间与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2024-12-22 22:12:10

您确定只有一次 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文