leutu
2024-06-11 a8c38350bcba205e98bf0b4e15e7bd41cde48bdd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 ;
    }
}