I need to upload some data on a site, using a POST request. I know to use HTTP client to execute a POST request
The problem is that in order to do this, you should authenticate first.
The site is a simple page prompting for a username and a password. I assume it stores a cookie in the browser and checks subsequent requests to see if I'm already authenticated.
But I don't have a concrete idea how to implement this on Android.
The client just gave me this:
URL to upload: http://xyz.com/?page=add
Credentials: admin/admin
Format of data:
$_POST = {
["Name"]=>string(255)
["Address"]=>string(255)
["ZIP"]=>string(50)
["City"]=>string(100)
["Phone"]=>string(50)
["Email"]=>string(50)
["Age"]=>int(11)
["Validation_Result"]=>string(255)
["Comment"]=>string(-)
}
$_FILES["Image"] = {
["name"]=>string "3D-graphics_3D_Triangles_006790_.jpg"
["type"]=>string "image/jpeg"
["tmp_name"]=>string "C:\Windows\Temp\php1362.tmp"
["error"]=>int(0)
["size"]=>int
}
And nothing else.
Could you please point me in the right direction how I would go about doing this?
How to do HTTP authentication in android?
Check out the top answer on this question. Very good explanation.
If you are doing the POST using
HttpClient
as the post you linked describes, you can add Basic Authentication by doing the following:HTH
I know this is a very old question, but this was the top search result I kept running into and I wanted to add another way that I managed to do this using CookieStore and HttpClient.
For my use case (Tomcat server configuration), I was hitting my base authenticated URL to get the cookie, POSTing my auth data to the form submission endpoint, and then using the cookie for subsequent calls. Here's the simplified piece of code that got it working for me: