Java代码OkHttpClient的使用

1.pom依赖

   <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.14.9</version>
  </dependency>

2、代码实现

public class OkHttpClientPost {
    //json传输方式
    private final static MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    //获取okHttpClient对象
    private final static OkHttpClient client = OkHttpClientObject.CLIENT
            .getClientInstance();
    
    /**
     * post形式
     */
     public static String post(String url, String message) throws IOException {
        //请求体传输json格式的数据
        RequestBody requestBody = RequestBody.create(JSON, message);
        //创建请求
        Request request = new Request.Builder()
                .url(url)
                //.header("User-Agent", "*****")
                //.addHeader("Accept", "*****")
                .post(requestBody)
                .build();

        Call call = client.newCall(request);
        okhttp3.Response response = call.execute();

        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }


    /**
     * post形式  multipart
     */
    public static String postMultipartFormData(String url, Map<String,String> paramMap) throws IOException {


        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        Set<String> keySet = paramMap.keySet();
        //拼接请求参数
        for (String key : keySet) {
            if (key.equals("image_file")) {
                String image_file = paramMap.get("image_file");
                byte[] bytes = new BASE64Decoder().decodeBuffer(image_file);
                RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), bytes);
                builder.addFormDataPart("image_file", "image_file", fileBody);
            } else {
                builder.addFormDataPart(key, paramMap.get(key));
            }
        }
        MultipartBody body = builder.build();
        //创建请求
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "multipart/form-data")
                .build();

        Call call = client.newCall(request);
        okhttp3.Response response = call.execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {

             int code = response.code();
             String message = response.message();
             log.error("OCR识别 响应状态码:{}  错误信息:{}",code,message);
            throw new IOException("Unexpected code " + response);
        }
    }







    /**
     * post形式
     */
    public static String postHeader(String url, String message,String header) throws IOException {
        //请求体传输json格式的数据
        RequestBody requestBody = RequestBody.create(JSON, message);
        //创建请求
        Request request = new Request.Builder()
                .url(url)
                .header("Authorization", header)
                //.addHeader("Accept", "*****")
                .post(requestBody)
                .build();

        Call call = client.newCall(request);
        okhttp3.Response response = call.execute();

        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值