java.net.SocketTimeoutException: Read timed out 10
httpclient4.4版本,HttpGet报错java.net.SocketTimeoutException:Readtimedout,本地测试是没问题的,线上报错,...
httpclient4.4版本,HttpGet报错java.net.SocketTimeoutException: Read timed out,本地测试是没问题的,线上报错,设置了超时时间也没用
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
StringBuilder sb = null;
String newUrl = URL+url;
try {
if(nvps == null || nvps.isEmpty()){
HttpGet httpGet = new HttpGet(newUrl);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(35000)
.setConnectionRequestTimeout(35000)
.setSocketTimeout(1000).build();
httpGet.setConfig(requestConfig);
httpGet.addHeader("token", TOKEN);
httpGet.addHeader("uid", UID);
response = httpclient.execute(httpGet);
}else{
HttpPost httpPost = new HttpPost(newUrl);
httpPost.addHeader("token", TOKEN);
httpPost.addHeader("uid", UID);
httpPost.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));
response = httpclient.execute(httpPost);
}
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String line = null;
sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
EntityUtils.consume(entity);
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(response!=null){
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(httpclient!=null){
try {
httpclient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
StringBuilder sb = null;
String newUrl = URL+url;
try {
if(nvps == null || nvps.isEmpty()){
HttpGet httpGet = new HttpGet(newUrl);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(35000)
.setConnectionRequestTimeout(35000)
.setSocketTimeout(1000).build();
httpGet.setConfig(requestConfig);
httpGet.addHeader("token", TOKEN);
httpGet.addHeader("uid", UID);
response = httpclient.execute(httpGet);
}else{
HttpPost httpPost = new HttpPost(newUrl);
httpPost.addHeader("token", TOKEN);
httpPost.addHeader("uid", UID);
httpPost.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));
response = httpclient.execute(httpPost);
}
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String line = null;
sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
EntityUtils.consume(entity);
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(response!=null){
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(httpclient!=null){
try {
httpclient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询