Java调用远程接口(传递json数据或传递文件及其它参数)
HttpClients调用接口传递json数据
package com.my.test;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class MyTest {
public static String test01(String path,Object obj) throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(path);
httpPost.setEntity(new StringEntity(JSON.toJSONString(obj),
ContentType.create("application/json", "UTF-8")));
CloseableHttpResponse execute = httpClient.execute(httpPost);
HttpEntity entity = execute