在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  網(wǎng)絡安全/ httpPost設置連接超時

httpPost設置連接超時

clipboard.png
這個要怎么設置連接超時,不是響應超時,HttpMethodParams.SO_TIMEOUT我看網(wǎng)上寫的這個代表請求超時,求指教

引用文字
回答
編輯回答
墨沫

你是用的HttpClient版本是多少?不通版本下設置超時有些差異,已為HttpClient 4.5例:

CloseableHttpClient httpclient = HttpClients.createDefault();  
HttpGet httpGet = new HttpGet("http://stackoverflow.com/");  
RequestConfig requestConfig = RequestConfig.custom()  
        .setConnectTimeout(5000).setConnectionRequestTimeout(1000)  
        .setSocketTimeout(5000).build();  
httpGet.setConfig(requestConfig);  
CloseableHttpResponse response = httpclient.execute(httpGet);  
System.out.println("得到的結果:" + response.getStatusLine());//得到請求結果  
HttpEntity entity = response.getEntity();//得到請求回來的數(shù)據(jù)

推薦你使用HttpClient配合使用Fluent-API:
https://hc.apache.org/httpcom...
設置超時:

// Execute a GET/POST with timeout settings and return response content as String.
Request.Get("http://somehost/")
        .connectTimeout(1000)
        .socketTimeout(1000)
        .execute().returnContent().asString();
2018年3月16日 14:31