请求老接口,还要带上账号、密码,一番考察觉得采用httpClient工具来搞
问题描述:
跑起来后调用一次接口没问题,再加了条数据就会卡在哪里阻塞,设置了超时时间也不行,每次都是第二个请求阻塞。
原因分析:
看到网上说有关JDK的BUG,添加了代码也不行,如(Java HttpClient execute 永久阻塞问题)想到是不是第一个请求没释放资源导致的,加了abort就可以了
解决方案:
import static java.util.stream.Collectors.toList;
import java.io.IOException;
import java.util.Calendar;
import java.util.stream.Stream;
import javax.validation.constraints.NotNull;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.config.SocketConfig;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class DigestHttpClientUtil {
private static Configs configs;
@Autowired
public void init(Configs configs) {
DigestHttpClientUtil.configs = configs;
}
public static String host = "http://xxxx:8080";
public static HttpClient digestHttpClient = digestHttpClient("111", "pass", null, null);
public static void updateUser() {
String userId = "test002";
HttpPut httpPut = new HttpPut(host + "/xxx/" + userId);
try {
httpPut.setHeader("Accept", "application/json");
httpPut.setHeader("Content-Type", "application/json");
httpPut.setEntity(new StringEntity(JSON.toJSONString(bo), "UTF-8"));
HttpResponse response = digestHttpClient.execute(httpPut);
System.out.println(response.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
} finally {
httpPut.abort(); //重点
}
}
public static HttpClient digestHttpClient(@NotNull String username, @NotNull String password, String host,
Integer port) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(StringUtils.isEmpty(host) ? AuthScope.ANY_HOST : host,
port == null ? AuthScope.ANY_PORT : port), new UsernamePasswordCredentials(username, password));
HttpClientBuilder cb = HttpClients.custom().setDefaultRequestConfig(
RequestConfig.custom().setConnectTimeout(60 * 1000).setSocketTimeout(60 * 1000).build());
SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(5000).build();
cb.setDefaultSocketConfig(socketConfig);
HttpClient client = cb.setDefaultCredentialsProvider(credentialsProvider).build();
return client;
}
public static HttpClient httpClient() {
return HttpClients.custom().build();
}
}



