I am developing two applications -
First one is web application using Spring MVC 3
And the second one is an Android application for same web application.
In both, I am integrating basic authentication to authenticate user on that site using the APIs.
In the API tutorial, following curl command is given to authenticate user -
$ curl -u username:password insert_Url_here -d '[xml body here]'
I am not getting, how to convert this command in Java and android code.
Please guide me. I am totally stuck here.
Using HttpClient 4, you will need to do the following:
CloseableHttpClient client = HttpClientBuilder.create().build();
final HttpPost postRequest = new HttpPost(SAMPLE_URL);
request.setEntity(new StringEntity("the body of the POST"));
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
postRequest.addHeader(new BasicScheme().authenticate(creds, request, null));
That's it - this is essentially what your curl
command does.
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.