[英]Convert Curl command into Java/Android
I am developing two applications - 我正在开发两个应用程序-
First one is web application using Spring MVC 3 第一个是使用Spring MVC 3的Web应用程序
And the second one is an Android application for same web application. 第二个是用于同一Web应用程序的Android应用程序。
In both, I am integrating basic authentication to authenticate user on that site using the APIs. 在这两种方法中,我都集成了基本身份验证以使用API在该站点上对用户进行身份验证。
In the API tutorial, following curl command is given to authenticate user - 在API教程中,以下curl命令用于验证用户身份-
$ curl -u username:password insert_Url_here -d '[xml body here]' $ curl -u用户名:密码insert_Url_here -d'[此处为xml正文]'
I am not getting, how to convert this command in Java and android code. 我不明白如何在Java和android代码中转换此命令。
Please guide me. 请指导我。 I am totally stuck here. 我完全被困在这里。
Using HttpClient 4, you will need to do the following: 使用HttpClient 4,您需要执行以下操作:
CloseableHttpClient client = HttpClientBuilder.create().build(); CloseableHttpClient客户端= HttpClientBuilder.create()。build();
final HttpPost postRequest = new HttpPost(SAMPLE_URL); 最终的HttpPost postRequest =新的HttpPost(SAMPLE_URL);
request.setEntity(new StringEntity("the body of the POST")); request.setEntity(new StringEntity(“ POST的主体”));
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); UsernamePasswordCredentials凭据=新的UsernamePasswordCredentials(用户名,密码);
postRequest.addHeader(new BasicScheme().authenticate(creds, request, null)); postRequest.addHeader(new BasicScheme()。authenticate(cred,request,null));
That's it - this is essentially what your curl
command does. 就是这样-本质上就是您的curl
命令执行的操作。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.