HttpClient/Downloader cancel/abort a request

HttpClient/Downloader cancel/abort a request


Here is my class using curl to download files: .h file .cpp file

you can set public variable downloading to false for canceling download.

Download a list of files from a url?

The question for the millon dollars is:

HttpClient provides a solution for it? :sunglasses:


Okay, your checking in every progress update callback, if the download was canceled. The same behavior could maybe manually be implemented into the Downloader (onTaskProgress ??) from cocos, but I don’t unterstand, why this isn’t by default?

@tranthor I believe currently will no one win the million dollar.


Hi @mars3142
If you want to abort the request then We are handling this via below code into destructor, you just need to save pointer to HttpRequest and set nullptr to responseCallback and call release.
We have multiple request so we store into vec.

 LoginMode::~LoginMode() {
	for (int i = 0;i < (int)httpVec.size();i++)


@smitpatel88 i’m trying to test your alternative.
How do you fill httpVec ? I’m trying like this but i’ve problems:

Declaration in my singleton class:
vector<cocos2d::network::HttpRequest*> httpVec;

In class method:

network::HttpRequest* request = new network::HttpRequest();
request->setResponseCallback([](network::HttpClient* client, network::HttpResponse* response) {




The error occurs in push_back line. Probably because of bad handle pointers.


As far, as I understand the code from @smitpatel88 you didn’t call release directly after creation, but within the loop of the destructor. But the code didn’t cancel long running downloads, it just remove the callback, so the response will never be processed. Right.


Yes, I understand like you, I never “cancel” the request but if I set nullptr is equal to cancel it. So, it could solve the “cancel” problem. But I wonder how can I fill httpVec… hmm…



@CAPONE sorry, I don’t understand your example.
How can I fill httpVec, following the example?

// cc @smitpatel88


As i told that we have multiple request so we have to use vec, but if you have single request then use this,

 LoginMode::~LoginMode() {


@smitpatel88 i’ve multiple request, how do you fill the vec? look my last post, i’ve an error when I try to insert in the vec using push_back


Are you taking this pointer as local variable? It must be global.
Declare in header file.


Thanks @smitpatel88 , that was the problem.
However, this alternative didn’t solve my problem.

My program crashes inside the for, at this line:>setResponseCallback(nullptr);

Sometimes in the first element, sometimes in the second, etc. but it crashes.
Maybe because if the http response is in “action” i can’t set null there.

i’m requesting http info when I touch a button.
but when i touch the “Close” button i would like to cancel all requests. Because of this, i put this code in “close” button action:

for (int i = 0;i < (int)httpVec.size();i++)

but it crashes when i touch it, as i said before.


Try giving callback method.
request->setResponseCallback(CC_CALLBACK_2(LoginMode::onHttpRequestCompleted, this));


Nope. I continue with the problem…


can you pls share me simple helloworld files to reproduce this?


I uploaded here an example.
As you can see, you’ve a green and red button.

If you touch green button, i’m trying to request 1000 cocos2d page (GET’s).
If you touch red button, i’m setting to NULL all elements in the vector.

If you touch green button and quicky you touch red button. The program crashes.


@smitpatel88 did you test it? if you can’t compile the project you could create a new project and copy classes folder.


Hey buddy, i am really sry.
I am very busy these days with 2 project.
But i will definitely check within 2-3 days and will revert you back.


this examples have some error, here updated code .h file .cpp file