В соответствии с моим примером кода, соединение должно быть заблокировано как установка 120 секунд, но оно заблокировало 84 секунды вместо 120 секунд, кто-нибудь может объяснить мне причину? Я не знаю, сталкивался ли кто-нибудь с такой же проблемой? Любые намеки или предложения приветствуются!
package httpclient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HttpClientExample1_1 {
public static void main(String[] args) throws IOException {
RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectionRequestTimeout(120000)
.setConnectTimeout(120000).setSocketTimeout(120000).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
long starttime = System.currentTimeMillis();
try {
HttpGet request = new HttpGet("https://google.com:81");
// add request headers
request.addHeader("custom-key", "test");
request.addHeader(HttpHeaders.USER_AGENT, "Googlebot");
CloseableHttpResponse response = httpClient.execute(request);
try {
// Get HttpResponse Status
System.out.println(response.getProtocolVersion()); // HTTP/1.1
System.out.println(response.getStatusLine().getStatusCode()); // 200
System.out.println(response.getStatusLine().getReasonPhrase()); // OK
System.out.println(response.getStatusLine().toString()); // HTTP/1.1 200 OK
HttpEntity entity = response.getEntity();
if (entity != null) {
// return it as a String
String result = EntityUtils.toString(entity);
System.out.println(result);
}
} finally {
response.close();
}
} finally {
long stoptime = System.currentTimeMillis();
System.out.println("Blocked time " + (stoptime - starttime) / 1000 + " seconds");
httpClient.close();
}
}
}