将Curl命令转换为Java/Android [英] Convert Curl command into Java/Android
问题描述
我正在开发两个应用程序-
I am developing two applications -
第一个是使用Spring MVC 3的Web应用程序
First one is web application using Spring MVC 3
第二个是用于同一Web应用程序的Android应用程序.
And the second one is an Android application for same web application.
在这两种方法中,我都集成了基本身份验证,以使用API在该站点上对用户进行身份验证.
In both, I am integrating basic authentication to authenticate user on that site using the APIs.
在API教程中,给出以下curl命令以对用户进行身份验证-
In the API tutorial, following curl command is given to authenticate user -
$ curl -u用户名:密码insert_Url_here -d'[此处为xml正文]'
我不明白如何在Java和android代码中转换此命令.
I am not getting, how to convert this command in Java and android code.
请指导我.我完全被困在这里.
Please guide me. I am totally stuck here.
推荐答案
使用HttpClient 4,您需要执行以下操作:
Using HttpClient 4, you will need to do the following:
- 创建客户端:
CloseableHttpClient客户= HttpClientBuilder.create().build();
CloseableHttpClient client = HttpClientBuilder.create().build();
- 创建POST请求:
- 设置您的POST请求的正文:
- 配置身份验证(大概是基本身份验证):
最终HttpPost postRequest =新的HttpPost(SAMPLE_URL);
final HttpPost postRequest = new HttpPost(SAMPLE_URL);
request.setEntity(new StringEntity("POST的主体"));
request.setEntity(new StringEntity("the body of the POST"));
UsernamePasswordCredentials凭据=新的UsernamePasswordCredentials(用户名,密码);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
postRequest.addHeader(new BasicScheme().authenticate(cred,request,null));
postRequest.addHeader(new BasicScheme().authenticate(creds, request, null));
就是这样-本质上就是您的curl
命令执行的操作.
That's it - this is essentially what your curl
command does.
希望这会有所帮助.
这篇关于将Curl命令转换为Java/Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!