在Java中,可以使用URLConnection或HttpClient来发送HTTP请求,并设置请求超时时间。
使用URLConnection:URL url = new URL("http://example.com");URLConnection connection = url.openConnection();connection.setConnectTimeout(5000); // 设置连接超时时间为5秒connection.setReadTimeout(5000); // 设置读取数据超时时间为5秒// 发送请求和处理响应...使用HttpClient:CloseableHttpClient httpClient = HttpClientBuilder.create().build();HttpParams params = httpClient.getParams();params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); // 设置连接超时时间为5秒params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); // 设置读取数据超时时间为5秒HttpGet httpGet = new HttpGet("http://example.com");CloseableHttpResponse response = httpClient.execute(httpGet);// 处理响应...以上代码示例分别使用URLConnection和HttpClient来发送HTTP请求,并设置连接超时时间和读取数据超时时间为5秒。可以根据实际需要调整超时时间。

