this.http.get的超时不能超过2分钟吗? [英] Can't have a timeout of over 2 minutes with this.http.get?

查看:496
本文介绍了this.http.get的超时不能超过2分钟吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的angular 4.3.2代码正在调用我的后端服务,该服务需要2-4分钟才能返回。我仅使用默认的 this.http.get 代码,即可看到默认超时在2分钟后开始。但是,当我尝试将超时时间设置为超过2分钟时,它将失败,因为它将永远不会使超时时间超过2分钟。

My angular 4.3.2 code is calling my back-end service that takes 2-4 minutes to return. Using just the default this.http.get code, I see that the default timeout kicks in after 2 minutes. However when I try to put in a timeout of anything OVER 2 minutes, it fails in that it will never let the timeout be over 2 minutes.

我尝试使用100、100000(170万)和114000(1.9m),但由于这些值,它们的超时时间恰到好处。但是当我尝试使用126000(2.1m),180000(3m)和1800000(30m)时,我又看到它在2分钟后超时。

I've tried with 100, 100000 (1.7m) and 114000(1.9m) and those work in that it gets timed out right at those values. But when I try 126000 (2.1m), 180000 (3m) and 1800000 (30m), again I see it times out after 2 minutes.

this.http.get('myUrl')
.timeout(126000)
.map((res: Response) => this.convertResponse(res));

我也尝试过使用 .timeoutWith(126000,Observable.throw (新错误(超时)))无济于事。

I've also tried it with .timeoutWith(126000, Observable.throw(new Error("Timed out"))) to no avail.

推荐答案

您不能更改HTTP请求的Web浏览器的网络超时设置。 timeout()运算符在到达计时器时引发JavaScript错误,但这与通信的网络超时无关。

You can not change the web browser's network timeout setting for HTTP requests. The timeout() operator throws a JavaScript error when the timer is reached, but this has nothing to do with the network timeout for communications.

例如;我可以在任何上使用 timeout()运算符。

For example; I can use the timeout() operator on any observable.

of("hello").pipe(delay(5000), timeout(1000));

以上内容将在1秒后超时。

The above will timeout after 1 second.

我的4.3.2角度代码正在调用我的后端服务,该服务需要2-4分钟才能返回

My angular 4.3.2 code is calling my back-end service that takes 2-4 minutes to return

服务器必须在2-4分钟的时间内传输HTTP标头和部分正文。这是继续HTTP连接所必需的,客户端无法采取任何措施使连接保持活动状态。

The server must transmit a HTTP header and a partial body during the duration of 2-4 minutes. This is required to continue the HTTP connection, and there is nothing the client can do to keep the connection alive.

HTTP请求无法完成是一种不好的做法很快。

It is a bad practice for a HTTP request to not complete quickly.

您可以要求服务器启动任务,然后按一定时间间隔轮询以查看任务是否完成,或者可以使用websocket与服务器通信并保持联系,直到完成为止。

You can either ask the server to start a task, and then poll on an interval to see if the task is complete, or you can use websockets to communicate with the server and remain connected until it is complete.

这两种方法都是广泛的话题,我无法提供更多细节。

Both approaches are broad topics and I can't go into more details than that.

这篇关于this.http.get的超时不能超过2分钟吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆