-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
core/src/main/java/com/flowci/core/common/manager/HttpRequestManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.flowci.core.common.manager; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.util.EntityUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.io.IOException; | ||
|
||
@Component("httpManager") | ||
public class HttpRequestManager { | ||
|
||
private final static int DefaultTimeout = 30 * 1000; | ||
|
||
private final static String DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"; | ||
|
||
private HttpClient client; | ||
|
||
@PostConstruct | ||
public void init() { | ||
RequestConfig config = RequestConfig.custom() | ||
.setConnectTimeout(DefaultTimeout) | ||
.setConnectionRequestTimeout(DefaultTimeout) | ||
.setSocketTimeout(DefaultTimeout) | ||
.build(); | ||
client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); | ||
} | ||
|
||
public String get(String url) throws IOException { | ||
HttpGet request = new HttpGet(url); | ||
request.setHeader("User-Agent", DefaultUserAgent); | ||
|
||
HttpResponse execute = client.execute(request); | ||
return EntityUtils.toString(execute.getEntity()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters