Я использую springboot для получения запроса от внешнего интерфейса, а затем отправляю другой запрос на другой адрес.
Но иногда появляется сообщение java.net.SocketTimeoutException: время соединения истекло, не каждый раз, когда возникает ошибка. Но когда я использовал почтальон для тестирования, я обнаружил, что запрос будет отвечать через 60 с, и я установил таймаут как 60 с. когда 60, но 8 вызвали проблему тайм-аута. Но 60-е годы - это не правильно, потому что в большинстве случаев запрос отвечает менее чем за 200 мс.
Я использую охттп 3.7
java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at okhttp3.internal.platform.Platform.connectSocket(Platform.java:124)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:221)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:147)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:192)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
at okhttp3.RealCall.execute(RealCall.java:69)
Метод в контроллере / сервисе springboot:
private HttpHelper httpHelper;
private static Logger logger = LoggerFactory.getLogger(ITSMServiceImpl.class);
private HttpHelper getHttpHelper() {
if (this.httpHelper == null) {
this.httpHelper = new HttpHelper();
}
return this.httpHelper;
}
@Override
public ITSMResult submitTicket(Ticket ticket, List<MultipartFile> files) throws SubmitException {
ITSMResult itsmResult = new Result();
Map<String, String> headers = new HashMap<>();
headers.put("SOAPACTION", "urn:Interface_INCFromIVA_WS");
String soupBody = this.generateSoupBody(ticket, files);
try {
Response response = this.getHttpHelper().executePostRequest(Constants.ITSM_END_POINT, headers, soupBody);
String bodyString = response.body().string();
logger.info(bodyString);
if (response.isSuccessful()) {
itsmResult = parseResponse(bodyString);
logger.info(itsmResult.getTicketId());
} else {
logger.error(response.body().string());
throw new XXXXException(ErrorCodes.ITSM_SUBIT_FAILURE, "提交失败,请重新提交");
}
} catch (IOException e) {
logger.error(e.getMessage());
e.printStackTrace();
throw new XXXXException(ErrorCodes.ITSM_SUBIT_FAILURE, "提交失败,请重新提交");
}
return itsmResult;
}
Код в Охтпхелпере:
import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.*;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import okhttp3.Request.Builder;
public class HttpHelper {
OkHttpClient client;
public HttpHelper() {
this.client = configureHttpClient();
}
/**
* @param username
* @param password
*/
public HttpHelper(String username, String password) {
this.client = configureHttpClient(username, password);
}
private OkHttpClient.Builder initDefaultBuilder() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
ConnectionSpec spec =
new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.allEnabledCipherSuites()
.build();
builder.connectionSpecs(Arrays.asList(spec, ConnectionSpec.CLEARTEXT));
final CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
builder.cookieJar(new CookieJar() {
private Map<String, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url.host(), cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url.host());
return cookies != null ? cookies : new ArrayList<Cookie>();
}
});
builder.connectTimeout(60, TimeUnit.SECONDS);
builder.writeTimeout(120, TimeUnit.SECONDS);
builder.readTimeout(240, TimeUnit.SECONDS);
return builder;
}
/**
* @return
*/
protected OkHttpClient configureHttpClient() {
return initDefaultBuilder().build();
}
protected OkHttpClient configureHttpClient(final String username, final String password) {
OkHttpClient.Builder builder = initDefaultBuilder();
builder.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(username, password);
return response.request().newBuilder().header("Authorization", credential).build();
}
});
return builder.build();
}
public Response executeGetRequest(String requestURL) throws IOException {
return this.executeGetRequest(requestURL, null);
}
public Response executeGetRequest(String requestURL, Map<String, String> headers) throws IOException {
Builder requestBuilder = new Request.Builder().url(requestURL).get();
if (headers != null && headers.size() > 0) {
for (String key : headers.keySet())
requestBuilder.addHeader(key, headers.get(key));
}
return client.newCall(requestBuilder.build()).execute();
}
public Response executePostRequest(String requestURL, Map<String, String> headers, String bodyString) throws IOException {
MediaType contentType = MediaType.parse(headers.get("Content-Type") + "; charset=utf-8");
RequestBody body = RequestBody.create(contentType, bodyString);
Builder requestBuilder = new Request.Builder()
.url(requestURL)
.post(body);
for (String key : headers.keySet())
requestBuilder.addHeader(key, headers.get(key));
return client.newCall(requestBuilder.build()).execute();
}
иногда возникает ошибка, я не нашел причину.