简单的网络请求工具类

网络请求工具类

基于springboot

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

代码如下
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**

  • 网络请求工具类
    */
    public class OkHttpUtil {

    private static final String MEDIA_TYPE = “application/x-www-form-urlencoded; charset=utf-8”;
    private static final String METHOD_GET = “GET”;
    private static final String METHOD_POST = “POST”;
    private static final String METHOD_PUT = “PUT”;
    private static final String METHOD_DELETE = “DELETE”;
    private static OkHttpClient okHttpClient;

    private OkHttpUtil() {}
    static{
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.readTimeout(10000, TimeUnit.MILLISECONDS);
    builder.writeTimeout(10000, TimeUnit.MILLISECONDS);
    builder.connectTimeout(10000, TimeUnit.MILLISECONDS);
    okHttpClient = builder.build();
    }
    /**

    • 异步post方法
    • @param url
    • @param baseRequest
    • @param callback
      */
      public static void enqueuePost(String url, String content, Callback callback) {
      Request request = createRequest(url, METHOD_POST, content);
      Call call = okHttpClient.newCall(request);
      call.enqueue(callback);

    }

    /**

    • 异步get方法
    • @param url
    • @param callback
      */
      public static void enqueueGet(String url, Callback callback) {
      Request request = createRequest(url, METHOD_GET, null);
      Call call = okHttpClient.newCall(request);
      call.enqueue(callback);

    }

    /**

    • 同步post方法
    • 注意此方法不能在主线程中调用
    • @param url
      */
      public static Response post(String url, String content) throws IOException {
      Request request = createRequest(url, METHOD_POST, content);
      Call call = okHttpClient.newCall(request);
      Response response = call.execute();
      return response;
      }

    /**

    • 同步get方法
    • 注意此方法不能在主线程中调用
    • @param url
      */
      public static Response get(String url) throws IOException {
      Request request = createRequest(url, METHOD_GET, null);
      Call call = okHttpClient.newCall(request);
      Response response = call.execute();
      return response;
      }

    /**

    • 创建request对象

    • @param url

    • @param method

    • @param baseRequest 项目中自己的的baseRequest对象

    • @return
      */
      private static Request createRequest(String url, String method, String content) {
      RequestBody requestBody = null;
      if (content != null) {
      MediaType JSON = MediaType.parse(MEDIA_TYPE);
      requestBody = RequestBody.create(JSON, content);

      }

      Request.Builder builder = new Request.Builder().url(url);

      Request request = null;

      switch (method) {
      case METHOD_GET:
      request = builder.build();
      break;
      case METHOD_POST:
      request = builder.post(requestBody).build();
      break;
      case METHOD_PUT:
      if (requestBody != null) {
      request = builder.put(requestBody).build();
      }
      break;
      case METHOD_DELETE:
      if (requestBody != null) {
      request = builder.delete(requestBody).build();
      } else {
      request = builder.delete().build();
      }
      break;
      }
      return request;
      }

    //测试使用
    public static void main(String[] args)throws Exception {
    Response response = post(“http://10.10.1.85:3000/mock/18/account/logout.html”, “clientId=e”);
    System.out.println(response.body().string());
    response = post(“http://10.10.1.85:3000/mock/18/account/login.html”, “brokerId=27&deptId=227&accountType=8&loginId=223100001178&loginPWD=991122&tradeId=223100001178&tradePWD=991122”);
    System.out.println(response.body().string());
    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值