import java.util.ArrayList;import java.util.List;import org.apache.http.Header;import org.apache.http.message.BasicHeader;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import com.alibaba.fastjson.JSON;public class HttpRequestDemo {private String baseUrl; private HttpClient httpClient; @Before public void setUp() { baseUrl = "http://localhost:8087/???"; httpClient = HttpClientBuilder.create().build(); }@Test public void testSend() throws Exception { String body = JSON.toJSONString(參數); HttpPost req = new HttpPost(baseUrl); List<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("頭部資訊", 值)); req.setHeaders(headers.toArray(new Header[0])); req.addHeader(HTTP.CONTENT_TYPE, "application/json");//請求參數為JSON格式 StringEntity entity = new StringEntity(body, "UTF-8"); req.setEntity(entity); HttpResponse resp = httpClient.execute(req); HttpEntity respEntity = resp.getEntity();//獲得返回資料 String text = EntityUtils.toString(respEntity, "UTF-8"); JSONObject obj = (JSONObject) JSONObject.parse(text); assertEquals(true, obj.get("success")); }}