-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOkHttp3tUtil.java
More file actions
71 lines (67 loc) · 2.28 KB
/
OkHttp3tUtil.java
File metadata and controls
71 lines (67 loc) · 2.28 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.yearcon.projectsystem.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import okhttp3.*;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* OkiHttp3工具类
*
* @return
* @author kemengkai
* @create 2020年10月21日
*/
@Service
@Slf4j
public class OkHttp3tUtil {
// 初始值
static final int timeOut = 10 * 1000;
private HttpUrl url = null;
OkHttpClient client = new OkHttpClient();
MediaType ContentType = null;
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final MediaType XML = MediaType.parse("application/xml; charset=utf-8");
@Modifying
@Transactional
public String Get(String url) {
this.url = HttpUrl.parse(url);
Request request = null;
if (this.url != null) {
request = new Request.Builder()
.url(this.url)
.build();
}
if (request != null) {
try (Response response = client.newCall(request).execute()) {
return response.body() != null ? response.body().string() : null;
} catch (IOException e) {
e.printStackTrace();
return "I/O错误";
}
}
return "链接请求错误!";
}
@Modifying
@Transactional
public String Post(String url, String ContentType, String data) {
this.url = HttpUrl.parse(url);
if (ContentType.equals("json")){
this.ContentType = JSON;
} else {
this.ContentType = XML;
}
RequestBody body = RequestBody.create(this.ContentType, data);
Request request = new Request.Builder()
.url(this.url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body() != null ? response.body().string() : null;
} catch (IOException e) {
e.printStackTrace();
return "I/O错误!";
}
}
}