Add post method. Add doRequest method

This commit is contained in:
chimp1984 2020-12-13 15:20:26 -05:00
parent 3cf97c706a
commit de2ba82b3d
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3
2 changed files with 26 additions and 14 deletions

View file

@ -30,6 +30,10 @@ public interface HttpClient {
@Nullable String headerKey, @Nullable String headerKey,
@Nullable String headerValue) throws IOException; @Nullable String headerValue) throws IOException;
String post(String param,
@Nullable String headerKey,
@Nullable String headerValue) throws IOException;
String getUid(); String getUid();
String getBaseUrl(); String getBaseUrl();

View file

@ -105,23 +105,34 @@ public class HttpClientImpl implements HttpClient {
public String requestWithGET(String param, public String requestWithGET(String param,
@Nullable String headerKey, @Nullable String headerKey,
@Nullable String headerValue) throws IOException { @Nullable String headerValue) throws IOException {
checkNotNull(baseUrl, "baseUrl must be set before calling requestWithGET"); return doRequest(param, HttpMethod.GET, headerKey, headerValue);
}
@Override
public String post(String param,
@Nullable String headerKey,
@Nullable String headerValue) throws IOException {
return doRequest(param, HttpMethod.POST, headerKey, headerValue);
}
private String doRequest(String param,
HttpMethod httpMethod,
@Nullable String headerKey,
@Nullable String headerValue) throws IOException {
checkNotNull(baseUrl, "baseUrl must be set before calling post");
Socks5Proxy socks5Proxy = getSocks5Proxy(socks5ProxyProvider); Socks5Proxy socks5Proxy = getSocks5Proxy(socks5ProxyProvider);
if (ignoreSocks5Proxy || socks5Proxy == null || baseUrl.contains("localhost")) { if (ignoreSocks5Proxy || socks5Proxy == null || baseUrl.contains("localhost")) {
return requestWithoutProxy(baseUrl, param, HttpMethod.GET, headerKey, headerValue); return requestWithoutProxy(baseUrl, param, httpMethod, headerKey, headerValue);
} else { } else {
return doRequestWithProxy(baseUrl, param, HttpMethod.GET, socks5Proxy, headerKey, headerValue); return doRequestWithProxy(baseUrl, param, httpMethod, socks5Proxy, headerKey, headerValue);
} }
} }
/** private String requestWithoutProxy(String baseUrl,
* Make an HTTP Get request directly (not routed over socks5 proxy). String param,
*/ HttpMethod httpMethod,
public String requestWithoutProxy(String baseUrl, @Nullable String headerKey,
String param, @Nullable String headerValue) throws IOException {
HttpMethod httpMethod,
@Nullable String headerKey,
@Nullable String headerValue) throws IOException {
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
String spec = baseUrl + param; String spec = baseUrl + param;
log.info("requestWithoutProxy: URL={}, httpMethod={}", spec, httpMethod); log.info("requestWithoutProxy: URL={}, httpMethod={}", spec, httpMethod);
@ -162,9 +173,6 @@ public class HttpClientImpl implements HttpClient {
} }
} }
/**
* Make an HTTP Get request routed over socks5 proxy.
*/
private String doRequestWithProxy(String baseUrl, private String doRequestWithProxy(String baseUrl,
String param, String param,
HttpMethod httpMethod, HttpMethod httpMethod,