简体   繁体   English

取消HTTP请求

[英]Cancel a HTTP request

I would like to know if there is any way I could check if a HTTP get is in pending status and if there is a way to cancel my HTTP get? 我想知道是否可以通过某种方式检查HTTP Get是否处于挂起状态以及是否可以取消HTTP Get?

This is my HTTP get: 这是我的HTTP获取:

public getData(): Observable<any> {
   return this.httpClient.get('http://IP_ADRESS:3490/testStream', {responseType: 'arraybuffer', observe: 'response' }});
}

public putData(): void {
   this.getData().subscribe((resp) => {
     console.log(resp);
     const responseArray: Uint8ClampedArray = new Uint8ClampedArray(resp);
     const newTabTemp: Uint8ClampedArray = new Uint8ClampedArray(this.maxHeight * this.maxWidth * 4);
     let comp = 0;
     for (let i = 0; i < this.maxHeight * this.maxWidth * 4; i = i + 4) {
       newTabTemp[i] = responseArray[comp];
       newTabTemp[i + 1] = responseArray[comp];
       newTabTemp[i + 2] = responseArray[comp];
       newTabTemp[i + 3] = 255;
       comp = comp + 1;
     }
     let nouvData: ImageData;
     nouvData = new ImageData(newTabTemp, this.maxWidth, this.maxHeight);
     this.contextVideo.putImageData(nouvData, BORDERX / 2, BORDERY / 2);
   });
}

要求花很多时间 请求从不成功

I'm getting a lot of these pending requests that never succeed or that take a long time to. 我收到了很多这些未决请求,这些请求永远不会成功或需要很长时间。 How can I check if they're pending and cancel them? 如何检查它们是否正在等待中并取消它们?

In Angular 2+, if you call unsubscribe on the request observable, the request will abort. 在Angular 2+中,如果您在观察到的请求上调用取消订阅,则请求将中止。 The natural behaviour of subscription.unsubscribe() is not to abort the request, but angular 2 guys have added this specific functionality in angular/http package. subscription.unsubscribe()的自然行为是不会中止请求,但是angular 2家伙已经在angular / http包中添加了此特定功能。 Please refer this Angular github repo. 请参阅 Angular github存储库。

In normal scenarios (when not using angular/http), if you want to abort the request on unsubscribe, you may leverage below code - 在正常情况下(当不使用angular / http时),如果要在退订时中止请求,则可以利用以下代码-

Observable.create((observer) => {

        function cancelRequest() {
            // Clear any even listeners added previously.

            request.abort();
        }

        // Other request processing related code

        request.send(formData);

        return () => {
            cancelRequest();
        };
    });

Unsubscribing from observable has same effect of cancellation. 取消订阅可观察项具有相同的取消作用。 In the example below the http request will be cancelled in 2s. 在下面的示例中,http请求将在2秒内取消。 You can view the full example here 您可以在此处查看完整的示例

search() {
const request = this.searchService.search(this.searchField.value)
  .subscribe(
    result => { this.result = result.artists.items; },
    err => { this.errorMessage = err.message; },
    () => { console.log('Completed'); }
  );

setTimeout(() => request.unsubscribe(), 2000);

} }

Cancelling an HTTP request is a common requirement. 取消HTTP请求是常见的要求。 For example, you could have a queue of requests where a new request supersedes a pending request and that pending request needs to be cancelled. 例如,您可能有一个请求队列,其中新请求取代了待处理请求,而该待处理请求需要取消。 Sign in to comment 登录以发表评论

To cancel a request we call the unsubscribe function of its subscription 要取消请求,我们调用其订阅的取消订阅功能

 @Component({ /* ... */ })
    export class AppComponent {
      /* ... */

      search() {
        const request = this.searchService.search(this.searchField.value)
          .subscribe(
            result => { this.result = result.artists.items; },
            err => { this.errorMessage = err.message; },
            () => { console.log('Completed'); }
          );

        request.unsubscribe();
      }
    }

Courtesy: Cancel http request 礼貌: 取消http请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM