OKhttp的get与post请求

1.post请求:

import okhttp3.*;
import org.apache.http.client.config.RequestConfig;

import java.util.HashMap;
import java.util.Map;

public class HttpClientTest {
    // 编码格式。发送编码格式统一用UTF-8
    private static final String ENCODING = "UTF-8";

    // 设置连接超时时间,单位毫秒。
    private static final int CONNECT_TIMEOUT = 6000;

    // 请求获取数据的超时时间(即响应时间),单位毫秒。
    private static final int SOCKET_TIMEOUT = 10000;
    public static String getResponse(String path,Map<String,String> params) throws Exception {

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
        OkHttpClient client = new OkHttpClient();
        MediaType json = MediaType.parse("application/json; charset=utf-8");
        RequestBody loginBody =
                RequestBody.create(json, com.alibaba.fastjson.JSON.toJSONString(params));

        // 创建一个请求
        Request request = new Request.Builder()
                .url(path)
                .post(loginBody)
                .addHeader("Content-Type", "application/json")
                .build();
        //给okhttpClient传入一个请求,这个时候得到一个call
        Call call = client.newCall(request);
        Response response = call.execute();
        int code = response.code();
        if (code == 200) {
//            System.out.println(response.body().string());
            return response.body().string();
        }
        return "500";
    }
}

调用方式:

String response = HttpClientTest.getResponse(loginPath,params);

2. get请求

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.apache.http.client.config.RequestConfig;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HttpClient {
    // 编码格式。发送编码格式统一用UTF-8
    private static final String ENCODING = "UTF-8";

    // 设置连接超时时间,单位毫秒。
    private static final int CONNECT_TIMEOUT = 6000;

    // 请求获取数据的超时时间(即响应时间),单位毫秒。
    private static final int SOCKET_TIMEOUT = 10000;
    public static String getResponse(String path) throws Exception {

        OkHttpClient client = new OkHttpClient();
        //构建一个请求对象
        Request request = new Request.Builder().url(path).build();
        //发送请求
        Response response = client.newCall(request).execute();
        //打印服务端传回的数据
        System.out.println(response.body().string());
        return response.body().string();
    }

    public static void main(String[] args) {
        HashMap<String,String> params = new HashMap<>() ;
        params.put("phoneNum","19939003136");

        try {
            String response = HttpClient.getResponse("http://172.16.20.46:9093/bllweb/usercenter/accmanage/phonetype?phonenum=19939003136");
            System.out.println("res------" + response);
            JSONObject json = JSONObject.parseObject(response);

            System.out.println("----------" + response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值