public class Send_Class {
public static int MAX_CONNECTION_PERROUTE = 1;//最大連接數
public static int SOCKET_TIMEOUT = 60000;//超時時間
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try {
String time1= sdf.format(new Date());
System.out.println(time1);
Send_Class sc=new Send_Class();
sc.test();
time1= sdf.format(new Date());
System.out.println(time1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void test(){
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(MAX_CONNECTION_PERROUTE);
Builder builder = RequestConfig.custom();
RequestConfig config = builder.setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(SOCKET_TIMEOUT)
.setConnectionRequestTimeout(SOCKET_TIMEOUT)
.setStaleConnectionCheckEnabled(true).build();
CloseableHttpClient client = HttpClients.custom()
.setMaxConnPerRoute(MAX_CONNECTION_PERROUTE).disableConnectionState()
.setDefaultRequestConfig(config)
.setConnectionManager(cm).build();
String url="http://128.28.28.28";
HttpPost post = new HttpPost(url);
post.setProtocolVersion(HttpVersion.HTTP_1_1);
post.setConfig(config);
List<NameValuePair> formpair = new ArrayList<NameValuePair>();
formpair.add(new BasicNameValuePair("name", "張三"));
HttpEntity entity = new UrlEncodedFormEntity(formpair, Consts.UTF_8);
if (entity != null) {
post.setEntity(entity);
}
try {
client.execute(post);
} catch (Exception e) {
e.printStackTrace();
}
}
}
httpclient最長只有20秒。你可以用循環增加長度