Search in sources :

Example 1 with HttpException

use of com.springboot.sample.util.okhttp.HttpException in project spring-boot-examples by tedburner.

the class OkHttpServiceImpl method GET.

@Override
public byte[] GET(String baseUrl, Map<String, String> queryParams) throws IOException {
    // 拼装param
    HttpUrl.Builder urlBuilder = HttpUrl.parse(baseUrl).newBuilder();
    for (Map.Entry<String, String> item : queryParams.entrySet()) {
        urlBuilder.addQueryParameter(item.getKey(), item.getValue());
    }
    HttpUrl httpUrl = urlBuilder.build();
    // 发送请求
    Request request = new Request.Builder().url(httpUrl.toString()).get().build();
    Response response = ReqExecute(request);
    if (!response.isSuccessful()) {
        throw new HttpException(response, "exception code:" + response.code());
    }
    return response.body().bytes();
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) HttpException(com.springboot.sample.util.okhttp.HttpException) Map(java.util.Map) HttpUrl(okhttp3.HttpUrl)

Example 2 with HttpException

use of com.springboot.sample.util.okhttp.HttpException in project spring-boot-examples by tedburner.

the class OkHttpServiceImpl method POST.

@Override
public byte[] POST(String url, String jsonBody) throws HttpException, IOException {
    RequestBody body = RequestBody.create(MEDIA_JSON, jsonBody);
    Request request = new Request.Builder().url(url).post(body).build();
    Response response = ReqExecute(request);
    if (!response.isSuccessful()) {
        throw new HttpException(response, "exception code:" + response.code());
    }
    return response.body().bytes();
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) HttpException(com.springboot.sample.util.okhttp.HttpException) RequestBody(okhttp3.RequestBody)

Aggregations

HttpException (com.springboot.sample.util.okhttp.HttpException)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 Map (java.util.Map)1 HttpUrl (okhttp3.HttpUrl)1 RequestBody (okhttp3.RequestBody)1