Android - Form based authentication

2019-08-16 00:32发布

问题:

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?

回答1:

How to do HTTP authentication in android?

Check out the top answer on this question. Very good explanation.



回答2:

If you are doing the POST using HttpClient as the post you linked describes, you can add Basic Authentication by doing the following:

String username, password;
DefaultHttpClient client = new DefaultHttpClient();

UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
client.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

HTH



回答3:

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:

String cookieUrl = "SOME_URL_THAT_WILL_PROVIDE_COOKIE";
String authenticateUrl = "URL_TO_POST_FORM_DATA";
String dataUrl = "AUTHENTICATED_URL_YOU_WANT_DATA_FROM";

final String userNameKey = "FORM_KEY_FOR_USERNAME";
final String userPassKey = "FORM_KEY_FOR_PASSWORD";
final String userName = "USER_NAME";
final String userPass = "USER_PASSWORD";

HttpClient client = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext context = new BasicHttpContext();
context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

String getUrl = cookieUrl;
HttpGet get = new HttpGet( getUrl );
HttpResponse getResponse = client.execute(get, context);
Log.d( "ConnectionTest", "GET @ " + getUrl );
Log.d( "ConnectionTest", getResponse.getStatusLine().toString() );

List<NameValuePair> authDataList = new ArrayList<NameValuePair>();
authDataList.add( new NameValuePair() {
    @Override
    public String getName() {
        return userNameKey;
    }

    @Override
    public String getValue() {
        return userName;
    }
} );
authDataList.add( new NameValuePair() {
    @Override
    public String getName() {
        return userPassKey;
    }

    @Override
    public String getValue() {
        return userPass;
    }
} );
HttpEntity authEntity = new UrlEncodedFormEntity( authDataList );

String authPostUrl = authenticateUrl;
HttpPost authPost = new HttpPost( authPostUrl );
authPost.setEntity( authEntity );
HttpResponse authPostResponse = client.execute(authPost, context);
Log.d( "ConnectionTest", "POST @ " + authPostUrl );
Log.d( "ConnectionTest", authPostResponse.getStatusLine().toString() );

String getUsersUrl = dataUrl;
HttpGet usersGet = new HttpGet( getUsersUrl );
HttpResponse usersGetResponse = client.execute(usersGet, context);
Log.d( "ConnectionTest", "GET @ " + getUsersUrl );
Log.d( "ConnectionTest", usersGetResponse.getStatusLine().toString() );
Log.d( "ConnectionTest", EntityUtils.toString( usersGetResponse.getEntity() ) );