HTTP Request with authenticate user - password

HTTP Request with authenticate user - password


hi everyone,

My project need to download zip file throught server links
But this server need user - password to download it
I will use cocos2d::network::HttpRequest* to download this zip file.
SO, how can i do it with authentication user -password ?
Please help me !

i would thanks you so much


It depends what the server itself expects in the request.
Does it want parameters? Pass it in as POST request data (setRequestData()).
Does it want it in the header? Set the headers first.
For auth on subsequent requests you’ll likely need to enable cookies as well (HttpClient::getInstance()->enableCookies()).


Thanks Jgod !
“Does it want it in the header? Set the headers first.”
-> So i’m still wondering about setting user-password in header each request.
Would you mind if you teach me by example ?
Thanks !


Username and password in headers is a non-standard practice for a server to have implemented (usually handles auth tokens there instead and user/password are passed in post args), but it would look something like this:

std::vector<std::string> headers;


Thanks Jgod,
if want to handle percent download ?
How can i do it !
pls help me


another question
Hi jgod,
it’s work for me.
But i wan to get bytes data downloaded.can we do it ?
because i known size of zip file download in constant. Accroding byte data downloaded in processing then we get % data downloading


I just checked the API for HttpRequest and HttpClient and there doesn’t appear to be any methods for checking that.

Take a look at AssetManager for downloading instead since it seems to have that built in as a callback (you’d subclass it and override the method):
virtual void onProgress(double total, double downloaded, const std::string &url, const std::string &customId);

Also, linking to your other post:


Thank Jgod,
I will try to check it !


Jgod hey !

Oh my god .
Httprequest i’m using to download Zip.

on MACOS XCode -> build to Iphone -> it run OK
When i build native to Andorid --> it’s failed
Httprequest run about 1 second and turn back to completed method . There aren’t data to be downloaded
i’m wonderring what’s it happend !

Please help me


Interesting, can you give the exact error message (if there is one)?
Are there any sort of logging statements to give more information?

Step through the debugger and see if there’s any relevant differences between the requests and responses on the two

Also make sure you have Internet permission enabled in the app’s manifest.

TLDR; need more info.


Thanks Jgod,

  • Here by my permission that using in my AndroidAndroidMantifest.xml

It’s still error breaking connect to download although response is OK from Server :sob:


You can use in this way :

    network::HttpRequest* request = new network::HttpRequest();
	char* out = NULL;
	std::string user_name = "abc";
	std::string user_pass = "xyz";
	base64Encode((unsigned char*)std::string(user_name+":"+user_pass).c_str(), std::string(user_name + ":" + user_pass).length(),&out);
	std::vector<std::string> headers;
	headers.push_back("Authorization : basic "+ std::string(out));
	request->setResponseCallback([](network::HttpClient *sender, network::HttpResponse *response) {

		if (response && response->getResponseCode() == 200 && response->getResponseData()) {
			std::vector<char> *data = response->getResponseData();
			std::string ret(&(data->front()), data->size());
			CCLOG("%s", ("Response message: " + ret).c_str());
		else {
			CCLOG("%s", ("Error " + std::to_string(response->getResponseCode()) + " in request").c_str()); 

	request->setTag("Get Authorization test");