修改HttpUtil
This commit is contained in:
14
pom.xml
14
pom.xml
@@ -148,6 +148,20 @@
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>0.9.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- OkHttp -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>4.8.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
<version>1.3.72</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package cn.celess.blog.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @Author: 小海
|
||||
@@ -14,8 +13,10 @@ import java.nio.charset.StandardCharsets;
|
||||
* @Desc:
|
||||
*/
|
||||
public class HttpUtil {
|
||||
private static final OkHttpClient CLIENT = new OkHttpClient();
|
||||
|
||||
public static String get(String urlStr) throws IOException {
|
||||
|
||||
/*public static String get(String urlStr) throws IOException {
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
@@ -48,4 +49,17 @@ public class HttpUtil {
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
*/
|
||||
public static String get(String urlStr) {
|
||||
Request request = new Request.Builder()
|
||||
.url(urlStr)
|
||||
.get()
|
||||
.build();
|
||||
try (Response response = CLIENT.newCall(request).execute()) {
|
||||
return Objects.requireNonNull(response.body()).string();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
22
src/test/java/cn/celess/blog/util/HttpUtilTest.java
Normal file
22
src/test/java/cn/celess/blog/util/HttpUtilTest.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package cn.celess.blog.util;
|
||||
|
||||
import cn.celess.blog.BaseTest;
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.Response;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class HttpUtilTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
String s = HttpUtil.get("https://api.celess.cn/headerInfo");
|
||||
assertNotNull(s);
|
||||
Response<Map<String, Object>> response = getResponse(s, MAP_OBJECT_TYPE);
|
||||
assertEquals(ResponseEnum.SUCCESS.getCode(), response.getCode());
|
||||
assertNotEquals(0, response.getResult().size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user