-
GET请求
参数直接拼接在 url 后/** * HTTP接口-GET方式,请求参数形式为params形式 * * @param url * @param param * @return String */ public static String sendGet(String url, String param) throws IOException { OkHttpClient client = new OkHttpClient().newBuilder() .connectTimeout(180, TimeUnit.SECONDS) .readTimeout(180, TimeUnit.SECONDS) .writeTimeout(180, TimeUnit.SECONDS) .build(); String dataUrl = url + "?userName=" + param; Request request = new Request.Builder() .url(dataUrl) .get() .build(); Response response; String result; try { response = client.newCall(request).execute(); result = response.body().string(); } catch (IOException e) { throw new IOException(DATA, e); } return result; }
-
参数为表单形式的POST请求
/** * 表单形式post请求 * * @paramurl 请求地址 * @parammap post请求参数 * @return请求结果 */ public static String doPostWithForm(Map<String, String> map, String url) throws IOException { 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请求
/** * HTTP接口-POST方式,请求参数形式为body-json形式 * * @param url * @param jsonString * @return String */ public static String sendPostWithJson(String url, String jsonString) throws IOException { OkHttpClient client = new OkHttpClient().newBuilder().build(); RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonString); Request request = new Request.Builder() .post(body) .url(url) .build(); Call call = client.newCall(request); //返回请求结果 try { Response response = call.execute(); return response.body().string(); } catch (IOException e) { throw new IOException(e); } }
-
参数直接拼接在url后面的POST请求
同GET 请求类似/** * HTTP接口-POST方式,请求参数形式为params形式 * * @param url * @param param * @return String */ public static String sendPost(String url, String param) throws IOException { OkHttpClient client = new OkHttpClient().newBuilder() .connectTimeout(180, TimeUnit.SECONDS) .readTimeout(180, TimeUnit.SECONDS) .writeTimeout(180, TimeUnit.SECONDS) .build(); FormBody body = new FormBody.Builder().build(); String dataUrl = url + "?userName=" + param; Request request = new Request.Builder() .url(dataUrl) .post(body) .build(); Response response; String result; try { response = client.newCall(request).execute(); result = response.body().string(); } catch (IOException e) { throw new IOException(e); } return result; }
OkHttp3发起POST或GET请求
最新推荐文章于 2024-09-18 21:29:21 发布
该代码示例展示了如何使用Java的OkHttpClient库进行GET和POST请求。对于GET请求,参数直接拼接在URL后面;对于POST,有表单形式和JSON形式,以及参数拼接在URL后的POST请求。所有请求都设定了超时时间,并处理了IOException。
摘要由CSDN通过智能技术生成