Basic authentication on Android

I found an example

try { String data = "YOUR REQUEST BODY HERE"; // CredentialsProvider credProvider = new BasicCredentialsProvider(); credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("YOUR USER NAME HERE", "YOUR PASSWORD HERE")); // DefaultHttpClient http = new DefaultHttpClient(); http.setCredentialsProvider(credProvider); // HttpPut put = new HttpPut("YOUR HTTPS URL HERE"); try { put.setEntity(new StringEntity(data, "UTF8")); } catch (UnsupportedEncodingException e) { Log.e(TAG, "UnsupportedEncoding: ", e); } put.addHeader("Content-type","SET CONTENT TYPE HERE IF YOU NEED TO"); HttpResponse response = http.execute(put); Log.d(TAG, "This is what we get back:"+response.getStatusLine().toString()+", "+response.getEntity().toString()); } catch (ClientProtocolException e) { // Log.d(TAG, "Client protocol exception", e); } catch (IOException e) { // Log.d(TAG, "IOException", e); } 

but I have to send a string in the authorization format: <Login>@<ID>:<Passsword>

how to do it?

+4
source share
2 answers

Are you sure your server really uses basic authentication? If so, you should set credentials as follows:

 new UsernamePasswordCredentials(" Login@ID ", "Passsword"); 

Otherwise, check what the authentic authentication protocol is and implement it (send authentication information in the header either as part of the URL or as POST parameters, etc.)

0
source

You should first look at what HTTP Basic actually is: http://en.m.wikipedia.org/wiki/Basic_access_authentication#section_3

Your question is not directed to HTTP Basic and should not be used at all because it is inherently unsafe.

I suppose what you ask should be part of the url. Even with additional measures such as SSL, I still won’t insert the credentials in the URL.

HTTP Basic defines how the username and password should be encrypted (actually hashed). There is no way to get HTTP and the format you requested.

0
source

Source: https://habr.com/ru/post/1438724/


All Articles