HTTP Request with authenticate user - password

HTTP Request with authenticate user - password
0

#1

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


#2

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()).


#3

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 !


#4

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);

#5

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


#6

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


#7

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:


#8

Thank Jgod,
I will try to check it !


#9

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


#10

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.


#11

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:


#12

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();