常用http连接方法

1、get请求

    public static JSONObject jsonGetRequest(String url, String username, String password) throws AppException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.MINUTES)
                .readTimeout(5, TimeUnit.MINUTES)
                .addInterceptor(new BasicAuthInterceptor(username, password)) //连接密码校验
                .build();
        Request req = new Request.Builder()
                .url(url)
                .get()
                .build();
       try (Response response = client.newCall(req).execute()) {
            int status = response.code();
            if (status != RESPONSE_STATUS_OK) {
                throw new AppException("请求数据失败,服务错误,代码:" + status);
            }
            ResponseBody responseBody = response.body();
            if(responseBody == null){
                throw new AppException("请求数据失败,服务端返回数据类型不正确");
            }
            String content = responseBody.string();
            if (StringUtils.isBlank(content)) {
                throw new AppException("请求数据失败,服务端返回数据类型不正确");
            }
            JSONObject json = JSONObject.parseObject(content);
            if (json == null) {
                throw new AppException("请求数据失败,服务端返回数据类型不正确");
            }
            return json;
        } catch (IOException e) {
            log.error("okhttp3 post error >> ex = {}", e.getMessage());
            throw new AppException("请求数据失败:" + e.getMessage());
        }
    }

2、post请求

 private void post(String url, JSONObject postData) throws AppException{

        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .addInterceptor(new BasicAuthInterceptor(username, password));
                .build();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(postData.toJSONString(), mediaType);

        Request req = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
        try (Response response = client.newCall(req).execute()) {
            int status = response.code();
            if (status != RESPONSE_STATUS_OK) {
                throw new AppException("请求数据失败,服务错误,代码:" + status);
            }
            ResponseBody responseBody = response.body();
            if (responseBody == null) {
                throw new AppException("请求数据失败,服务端返回数据类型不正确");
            }
            String content = responseBody.string();
            log.info("response from sync data server, data: {}", content);
            if (StringUtils.isBlank(content)) {
                throw new AppException("请求数据失败,服务端返回数据类型不正确");
            }
            JSONObject json = JSONObject.parseObject(content);
            if (json == null) {
                throw new AppException("请求数据失败,服务端返回数据类型不正确");
            }
            if (0 != json.getInteger("error")) {
                throw new AppException("请求数据失败," + json.getString("msg"));
            }
        } catch (IOException e) {
            log.error("okhttp3 post error >> ex = {}", e.getMessage(), e);
            throw new AppException("请求数据失败:" + e.getMessage());
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值