java HttpClient
import com.mashape.unirest.http.*; public class main { public static void main(String []args) throws Exception{ Unirest.setTimeouts(0, 0); HttpResponse<String> response = Unirest.post("http://10.194.89.13:8888/getAutoAirRealtimeData") .header("accessToken", "2D636E19-FC4D-45AB-9C55-584DB3F9AF4B") // .multiPartContent() .field("start_time", "2021-11-01 00:00:00") .field("sign", "E06ACCDA7BAF454A08DDB8BE8AD18DE6D915A2A7") .field("end_time", "2021-11-01 01:00:00") .field("type", "1") .field("mpId", "1,2,3,4,5,6,7") .field("_ts", "1635923750") .asString(); System.out.println(response.getBody()); } }
pom.xml:
<dependency> <groupId>com.mashape.unirest</groupId> <artifactId>unirest-java</artifactId> <version>1.4.9</version> </dependency>
成功
二:
package com.orisdom; import com.alibaba.fastjson.JSONObject; import lombok.SneakyThrows; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.junit.Test; public class http { @SneakyThrows @Test public void httppost() { HttpClient httpClient = new DefaultHttpClient(); HttpPost pmethod = new HttpPost("http://10.194.89.13:8888/getAutoAirRealtimeData"); // pmethod.addHeader("Connection", "keep-alive"); pmethod.addHeader("Accept", "*/*"); pmethod.addHeader("Content-Type", "multipart/form-data;boundary=<calculated when request is sent>"); // pmethod.addHeader("Content-Length", "xmlRequest.getBytes().length+"); pmethod.addHeader("Host", "10.194.89.13:8888"); // pmethod.addHeader("X-Requested-With", "XMLHttpRequest"); pmethod.addHeader("Connection", "keep-alive"); pmethod.addHeader("User-Agent", "PostmanRuntime/7.26.8"); pmethod.addHeader("accessToken", "2D636E19-FC4D-45AB-9C55-584DB3F9AF4B"); JSONObject obj = new JSONObject(); obj.put("start_time", "2021-11-01 00:00:00"); obj.put("sign", "E06ACCDA7BAF454A08DDB8BE8AD18DE6D915A2A7"); obj.put("end_time", "2021-11-01 01:00:00"); obj.put("type", "1"); obj.put("mpId", "1,2,3,4,5,6,7"); obj.put("_ts", "1635923750"); pmethod.setEntity(new StringEntity(obj.toString(),"UTF-8")); HttpResponse response = httpClient.execute(pmethod); int code = response.getStatusLine().getStatusCode(); if (code == 200) { System.out.println("ok"); System.out.println(EntityUtils.toString(response.getEntity())); } // // CloseableHttpClient httpClient = HttpClients.createDefault(); // // 创建 HttpPost 请求 // HttpPost httpPost = new HttpPost("http://10.194.89.13:8888/getAutoAirRealtimeData"); // // 设置长连接 // httpPost.setHeader("Connection", "keep-alive"); // // 设置认证信息 // httpPost.setHeader("accessToken", "2D636E19-FC4D-45AB-9C55-584DB3F9AF4B"); //// httpPost.setHeader("Authorization", "2D636E19-FC4D-45AB-9C55-584DB3F9AF4B"); // httpPost.addHeader("Content-Type", "multipart/form-data;boundary=<calculated when request is sent>"); // httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"); // httpPost.setHeader("Host", "10.194.89.13:8888"); // // JSONObject obj = new JSONObject(); // obj.put("start_time", "2021-11-01 00:00:00"); // obj.put("sign", "E06ACCDA7BAF454A08DDB8BE8AD18DE6D915A2A7"); // obj.put("end_time", "2021-11-01 01:00:00"); // obj.put("type", "1"); // obj.put("mpId", "1,2,3,4,5,6,7"); // obj.put("_ts", "1635923750"); // // CloseableHttpResponse httpResponse = null; // try { // StringEntity entity = new StringEntity(obj.toJSONString(), "utf-8"); // entity.setContentType("application/json"); // httpPost.setEntity(entity); // httpResponse = httpClient.execute(httpPost); // HttpEntity httpEntity = httpResponse.getEntity(); // //打印结果 // System.out.println(EntityUtils.toString(httpEntity)); // } catch (IOException e) { // e.printStackTrace(); // } finally { // try { // if (httpResponse != null) { // httpResponse.close(); // } // } catch (IOException e) { // e.printStackTrace(); // } // // try { // if (httpClient != null) { // httpClient.close(); // } // } catch (IOException e) { // e.printStackTrace(); // } // } } }
pom.xml:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.6</version> </dependency>
失败,但也是一种方法