package com.yb.util;
|
|
import okhttp3.*;
|
import org.springframework.stereotype.Component;
|
|
import java.io.IOException;
|
@Component
|
public class OkHttpUtil {
|
// get请求
|
public String getMessage(String url) {
|
try {
|
OkHttpClient client = new OkHttpClient();
|
Request request = new Request.Builder()
|
.url(url)
|
.build();
|
|
Response response = client.newCall(request).execute();
|
return response.body().string();
|
}catch (IOException e){
|
System.out.println(e.getMessage());
|
}
|
return null ;
|
}
|
|
// post请求
|
public String postMessage(String url, String params) {
|
try {
|
OkHttpClient client = new OkHttpClient();
|
RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params);
|
Request request = new Request.Builder()
|
.url(url)
|
.post(body)
|
.build();
|
|
Response response = client.newCall(request).execute();
|
return response.body().string();
|
|
} catch (IOException e) {
|
System.out.println(e.getMessage());
|
}
|
return null ;
|
}
|
}
|