我写了一个Java客户端,可以执行http
GET请求,没有任何问题。
现在我想修改这个客户端,以便执行https
GET请求。
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
private String executeGet(final String url, String proxy, int port)
throws IOException, RequestUnsuccesfulException, InvalidParameterException {
CloseableHttpClient httpclient = null;
String ret = "";
RequestConfig config;
try {
String hostname = extractHostname(url);
logger.info("Hostname {}", hostname);
HttpHost target = new HttpHost(hostname, 80, null);
HttpHost myProxy = new HttpHost(proxy, port, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(USERNAME, PASSWORD));
httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
config = RequestConfig.custom().setProxy(myProxy).build();
HttpGet request = new HttpGet(url);
request.setConfig(config);
CloseableHttpResponse response = httpclient.execute(target, request);
...
我原本以为只需要使用 HttpsGet
替换 HttpGet
就可以了,但是事实上并没有可用的 HttpsGet
类。
最简单的方法是如何修改这个方法以处理 https
的 GET 请求?