okhttp3 参数形式不同的post请求

发布于:2024-06-06 ⋅ 阅读:(161) ⋅ 点赞:(0)
 <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.10.0</version>
        </dependency>

 下面是参数是Map的形式请求结果:

public static String post(Map<String, String> map,  String url) throws IOException {
        if(StringUtils.isEmpty(url)){
            return "";
        }
        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(180, TimeUnit.SECONDS)
                .readTimeout(180, TimeUnit.SECONDS)
                .writeTimeout(180, TimeUnit.SECONDS)
                .build();
        //构建一个formBody builder
        FormBody.Builder builder = new FormBody.Builder();
        //循环form表单,将表单内容添加到form builder中
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            builder.add(key, value);
        }
        //构建formBody,将其传入Request请求中
        FormBody body = builder.build();
        Request request = new Request.Builder().url(url).post(body).build();
        Call call = client.newCall(request);
        //返回请求结果
        try (Response response = call.execute()) {
            return response.body().string();
        } catch (IOException e) {
            throw new IOException(e);
        }
    }

 下面是参数是Json的形式请求结果: 

/**
     * Post请求发送JSON数据....{"name":"zhangsan","pwd":"123456"}
     * 参数一:请求Url
     * 参数二:请求的JSON
     * 参数三:请求回调
     */
    public static String postJsonParams(String url, String jsonParams) {
        try {
            log.info("接口参数-->"+jsonParams);
            OkHttpClient client = new OkHttpClient.Builder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, jsonParams);
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            if (response.code() == 307) {
                String redirectUrl = response.header("Location");
                System.out.println("重定向URL:" + redirectUrl);

                Request redirectedRequest = request.newBuilder()
                        .url(redirectUrl)
                        .build();
                Response redirectedResponse = client.newCall(redirectedRequest).execute();

                String responseBody = redirectedResponse.body().string();
                log.info("接口返回-->"+responseBody);
                return responseBody;
            } else {
                String responseBody = response.body().string();
                return responseBody;
            }
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }

    }


网站公告

今日签到

点亮在社区的每一天
去签到