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;
headers.push_back("username=123123");
headers.push_back("password=abc");
request->setHeaders(headers);
request->setRequestType(HttpRequest::Type::POST);

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
platforms.

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();
	request->setRequestType(network::HttpRequest::Type::GET);
	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->setHeaders(headers);
	
	request->setUrl("http://url/api/");
	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");
	network::HttpClient::getInstance()->send(request);

	request->release();
1 Like

Maybe it will be helpful for some one. I tried @itsabhiaryan solution, but it not worked for me. Problem was in the extra spaces in the header string. Also i suggest you use modern c++ buffer alocation: std::vector<char> buffer(size)

Example

#define HTTP_AUTH "xxx:xxx"

...

    std::vector<char*> buffer(256);
    base64Encode( (unsigned char*)(HTTP_AUTH), sizeof(HTTP_AUTH), buffer.data() );
    std::vector<std::string> headers;
    headers.push_back("Authorization:Basic "+ std::string(*buffer.data()));
    request->setHeaders(headers);
    buffer.clear();

...
1 Like

Why do you use char* for buffer?
Looks strange, don’t see reason for it

Yes you right. My mistake was that i didn’t look cocos’s base64Encode implementation. It takes unsigned char** pointer to store 64base encoded memory with terminating ‘\0’ character. Also memory is expected to be freed. So my new code look like

#define HTTP_AUTH "xxx:xxx"

...

    char *out = nullptr;
    auto n = base64Encode( (const unsigned char*)(HTTP_AUTH), sizeof(HTTP_AUTH), &out );
    std::string htpasswd( out, (std::string::size_type)n );
    free(out);
    std::vector<std::string> headers;
    headers.push_back("Authorization:Basic " + htpasswd);
    request->setHeaders(headers);

...
1 Like