pom.xml中添加以下jar
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.59</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
代码如下
package com.example.demo;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
@SpringBootTest
class DemoApplicationTests {
private final Logger log = LoggerFactory.getLogger(this.getClass());
/*@Test
public void post() throws Exception{
try {
String uri = "http://112.30.157.105:8082/IOTWebService/rest/oauth2/token";
PostMethod postMethod = new PostMethod(uri);
postMethod.setRequestHeader("content-type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
NameValuePair[] nameValuePairs = {
new NameValuePair("client_id", "60dcb643-657b-49a8-b820-2c75b236edba"),
new NameValuePair("client_secret", "752d37c6-33ea-4a27-b856-05a9c67c4572"),
new NameValuePair("grant_type", "client_credentials")
};
postMethod.setRequestBody(nameValuePairs);
HttpClient httpClient = new HttpClient();
int responseCode = httpClient.executeMethod(postMethod);
System.out.println("响应码:" + responseCode);
String responseBodyAsString = postMethod.getResponseBodyAsString();
System.out.println(responseBodyAsString);
} catch (Exception e) {
e.printStackTrace();
}
}*/
@Test
public void post() throws Exception{
try {
String uri = "http://112.30.157.105:8082/IOTWebService/rest/RealNameDataService/UploadAttendanceInfo?access_token=a7d3185996880cd94d965b680feb247b";
PostMethod postMethod = new PostMethod(uri);
postMethod.setRequestHeader("content-type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
NameValuePair[] nameValuePairs = {
new NameValuePair("params", "" +
"{" +
" \"unifiedProjectCode\":\"03ae96c7-0a4f-4a55-b926-4a9335ad13be\"," +
" \"idCardNumber\":\"210202198506021378\"," +
" \"attendanceDate\":\"2020-08-08 08:00:00\"," +
" \"direction\":\"direction\"," +
" \"image\":\"1\"," +
" \"deviceSN\":\"ZXHJSB0810\"," +
" \"dataTimeStamp\":\"1591231369050\"," +
" \"temparature\":\"38\"" +
"}")
};
postMethod.setRequestBody(nameValuePairs);
HttpClient httpClient = new HttpClient();
int responseCode = httpClient.executeMethod(postMethod);
System.out.println("响应码:" + responseCode);
String responseBodyAsString = postMethod.getResponseBodyAsString();
System.out.println(responseBodyAsString);
} catch (Exception e) {
e.printStackTrace();
}
}
/*@Test
public void post3() throws Exception{
try {
String uri = "http://112.30.157.105:8082/IOTWebService/rest/EnvironmentDataService/PushRealTimeInfo?access_token=a7d3185996880cd94d965b680feb247b";
PostMethod postMethod = new PostMethod(uri);
postMethod.setRequestHeader("content-type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
NameValuePair[] nameValuePairs = {
new NameValuePair("params", "" +
"{" +
" \"unifiedProjectCode\":\"03ae96c7-0a4f-4a55-b926-4a9335ad13be\"," +
" \"dataTimeStamp\":\"1591231369050\"," +
" \"deviceFactory\":\"GTXD实施\"," +
" \"deviceSN\":\"ZXHJSB0810\"," +
" \"tsp\":\"60\"," +
" \"pressure\":\"60\"," +
" \"windDirection\":\"90\"," +
" \"windSpeed\":\"30\"," +
" \"windLevel\":\"4\"," +
" \"temperature\":\"30\"," +
" \"humidity\":\"50\"," +
" \"noise\":\"40\"," +
" \"pm10\":\"100\"," +
" \"pm25\":\"100\"," +
" \"atmospheric\":\"12\"," +
" \"direction\":\"12\"," +
" \"date\":\"2020-08-30\"," +
" \"time\":\"09:04:04\"," +
" \"protocolVer\":\"protocolVer\"," +
" \"otherField\":\"\"" +
"}")
};
postMethod.setRequestBody(nameValuePairs);
HttpClient httpClient = new HttpClient();
int responseCode = httpClient.executeMethod(postMethod);
System.out.println("响应码:" + responseCode);
String responseBodyAsString = postMethod.getResponseBodyAsString();
System.out.println(responseBodyAsString);
} catch (Exception e) {
e.printStackTrace();
}
}*/
}
效果图