POST接口测试练习

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class post3 {
    /**
     * 2023年2月14日20:36:21
     * POST请求示例,练习
     * 博客地址如下:
     * https://blog.csdn.net/TiankkTT/article/details/126685422?ops_request_misc=&request_id=&biz_id=102&utm_term=HttpURLConnection&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-4-126685422.nonecase&spm=1018.2226.3001.4187
     */
    public static void main(String[] args) {
        try {
            // 1. 获取访问地址URL
            URL url = new URL("http://192.168.2.7:8080/j_spring_security_check");
            // 2. 创建HttpURLConnection对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            /* 3. 设置请求参数等 */
            // 请求方式
            connection.setRequestMethod("POST");
            // 设置连接超时时间(以毫秒为单位)
            connection.setConnectTimeout(3000);
            // 设置是否向 HttpUrlConnection 输出,对于post请求,参数要放在 http 正文内,因此需要设为true,默认为false。
            connection.setDoOutput(true);
            // 设置是否从 HttpUrlConnection读入,默认为true
            connection.setDoInput(true);
            // 设置是否使用缓存
            connection.setUseCaches(false);
            // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向
            connection.setInstanceFollowRedirects(true);
            // 设置使用标准编码格式编码参数的名-值对
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 添加 HTTP HEAD 中的一些参数。JDK8中,HttpURLConnection默认开启Keep-Alive
            // connection.setRequestProperty("Connection", "Keep-Alive");
            // 连接
            try {
                connection.connect();
            } catch (IOException e) {
                e.printStackTrace();
            }
            /* 4. 处理输入输出 */
            // 写入参数到请求中
            String params = "j_username=admin&j_password=admin&from=%2Fjob%2Ftest%2F&Submit=%E7%99%BB%E5%BD%95";
            OutputStream out = connection.getOutputStream();
            out.write(params.getBytes());
            out.flush();
            out.close();

            // 从连接中读取响应信息
            StringBuilder msg = new StringBuilder();
            int code = connection.getResponseCode();
            if (code == 200) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    msg.append(line).append("\n");
                }
                reader.close();
            }
            // 5. 断开连接
            connection.disconnect();
            // 处理结果
            System.out.println(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

说明:本地搭建Jenkins,验证登录接口

问题:设置登录接口的参数如下,可以正常使用:

String params = "j_username=admin&j_password=admin&from=%2Fjob%2Ftest%2F&Submit=%E7%99%BB%E5%BD%95";

但是使用JSONObject提交数据的时候为什么提交不上去?

JSONObject jso = new JSONObject();
jso.put("j_username", "admin");
jso.put("j_password", "admin");
jso.put("from", "");
jso.put("Submit", "%E7%99%BB%E5%BD%95");
jso.put("remember_me", "ON");
out.write(jso.toString().getBytes());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值