java请求post接口方法

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();
        }
    }*/

}

效果图

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哎呦喂O_o嗨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值