java怎么用httpclient调用第三方接口,详细代码
时间: 2023-05-25 16:05:28 浏览: 130
Java调用第三方接口示范的实现
以下是使用Httpclient调用第三方接口的详细代码:
1. 引入Httpclient库
首先需要在项目中引入Httpclient库,可以在Maven中添加如下依赖:
```
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
```
2. 创建HttpClient对象
```
CloseableHttpClient httpClient = HttpClients.createDefault();
```
3. 创建请求对象
```
String url = "http://api.xxx.com/user/info";
HttpPost httpPost = new HttpPost(url);
```
这个例子中,我们发送的请求是POST请求,URL是"http://api.xxx.com/user/info"。
4. 设置请求参数
如果发送POST请求,需要设置请求参数。我们可以创建一个List<NameValuePair>来存储参数,例如:
```
List<NameValuePair> formParams = new ArrayList<>();
formParams.add(new BasicNameValuePair("username", "test"));
formParams.add(new BasicNameValuePair("password", "123456"));
```
这个例子中,我们设置了两个参数,一个是"username",值为"test",另一个是"password",值为"123456"。
然后将参数设置到请求对象中:
```
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
httpPost.setEntity(formEntity);
```
5. 发送请求并获取响应
```
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
// 处理响应结果
} finally {
response.close();
}
```
首先通过httpClient的execute方法发送请求,得到响应对象response。在finally里将响应对象关闭,释放链接。
然后通过HttpEntity中的EntityUtils将响应实体entity转换成字符串类型,供后续处理。
6. 处理响应结果
得到响应结果之后,我们需要根据第三方接口的具体情况来做不同的处理。例如,如果第三方接口返回的是JSON数据,我们可以通过Jackson库将JSON字符串转换成POJO对象,例如:
```
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(result, User.class);
```
这个例子中,我们将响应结果result转换成了一个User对象。
7. 完整代码
```
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class HttpClientUtil {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "http://api.xxx.com/user/info";
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formParams = new ArrayList<>();
formParams.add(new BasicNameValuePair("username", "test"));
formParams.add(new BasicNameValuePair("password", "123456"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
httpPost.setEntity(formEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
// 处理响应结果
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(result, User.class);
System.out.println(user);
} finally {
response.close();
}
}
public static class User {
private String username;
private String password;
// getter, setter
}
}
```
阅读全文