java实现http请求具体代码

OkHttpClient client = new OkHttpClient();
String credential = Credentials.basic("用户名", "密码");
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&undefined=");
Request request = new Request.Builder()
        .url("url")
        .post(body)
        .addHeader("Authorization", credential)
        .build();
Response response = client.newCall(request).execute();
JSONObject jsonObject = JSON.parseObject(response.body().string());
String access_token = jsonObject.getString("access_token");
System.out.println(access_token);

 

posted @ 2022-05-12 17:44  xxfcode  阅读(335)  评论(0编辑  收藏  举报