Skip to content

Commit

Permalink
+ resolved #35
Browse files Browse the repository at this point in the history
  • Loading branch information
hsiong committed Aug 8, 2023
1 parent 56bfc9e commit bdeca4c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ A http client for java, based on `HttpURLConnection`. Support get/post/put/delet

Simple, effective, and highly expandable ~

# Deploy
## Snapshot
mvn clean deploy

## Release
mvn clean deploy -P release

# Maven Dependency
```
<dependency>
<groupId>tech.ynfy</groupId>
<artifactId>ynfy-tool-httpconnect</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
</dependency>
```

# Change Notes
+ [v1.1.4](https://github.com/ynfy-tech/ynfy-tool-httpconnect/releases/tag/v1.1.4)
+ resolved [#35](https://github.com/ynfy-tech/ynfy-tool-httpconnect/issues/35)
+ [v1.1.3](https://github.com/ynfy-tech/ynfy-tool-httpconnect/releases/tag/v1.1.3)
+ resolved [#33](https://github.com/ynfy-tech/ynfy-tool-httpconnect/issues/33)
+ Optimize code
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>tech.ynfy</groupId>
<artifactId>ynfy-tool-httpconnect</artifactId>
<name>${project.groupId}:${project.artifactId}</name>
<version>1.1.3</version>
<version>1.1.4</version>
<description>A Http Util for Java</description>
<url>https://github.com/ynfy-tech/ynfy-tool-httpconnect</url>

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/http/InitUtil.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package http;


import http.constant.Constant;
import http.constant.ProtocolConstant;
import http.constant.TimeConstant;
import http.constant.HttpEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -104,6 +104,10 @@ protected String sendPostOrPutConnection(String url, HttpEnum httpEnum, Map<Stri
* @return HttpURLConnection
*/
protected HttpURLConnection getConnection(String url) {
if (!url.contains(ProtocolConstant.PROTOCOL_HTTP)) {
// 如果 url 不包含 http 协议头, 则自动拼接 http; 兼容 https; 尽可能实现开箱可用
url = ProtocolConstant.PROTOCOL_HTTP_PREFIX + url;
}
// 打开和URL之间的连接
HttpURLConnection conn = null;
try {
Expand All @@ -113,8 +117,8 @@ protected HttpURLConnection getConnection(String url) {
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json; utf-8");
conn.setConnectTimeout(Constant.CONNECT_TIME_OUT);
conn.setReadTimeout(Constant.READ_TIME_OUT);
conn.setConnectTimeout(TimeConstant.CONNECT_TIME_OUT);
conn.setReadTimeout(TimeConstant.READ_TIME_OUT);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
Expand All @@ -134,8 +138,8 @@ protected HttpURLConnection getConnection(String url, HttpEnum httpEnum) {
HttpURLConnection connection = getConnection(url);
try {
connection.setRequestMethod(httpEnum.name());
connection.setConnectTimeout(Constant.CONNECT_TIME_OUT);
connection.setReadTimeout(Constant.READ_TIME_OUT);
connection.setConnectTimeout(TimeConstant.CONNECT_TIME_OUT);
connection.setReadTimeout(TimeConstant.READ_TIME_OUT);
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true);
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/http/constant/ProtocolConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package http.constant;

/**
* 〈〉
*
* @author Hsiong
* @version 1.0.0
* @since 2023/8/8
*/
public interface ProtocolConstant {

/**
* 协议 - http - 关键字
*/
String PROTOCOL_HTTP = "http";

/**
* 协议 - http - 前缀
*/
String PROTOCOL_HTTP_PREFIX = "http://";

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @version 1.0.0
* @since 2022/12/15
*/
public interface Constant {
public interface TimeConstant {

/**
* 连接超时
Expand Down

0 comments on commit bdeca4c

Please sign in to comment.