Java爬虫類HttpClient--構成要求パラメータ
10146 ワード
package xxx.xxx.xxx;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HttpConfigTest {
public static void main(String[] args) {
//1. , HTTPClient
CloseableHttpClient httpClient = HttpClients.createDefault();
//2. , get HttpGet
HttpGet httpGet = new HttpGet("http://www.itcast.cn");
//
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // ,
.setConnectionRequestTimeout(500) // ,
.setSocketTimeout(10*1000) // ,
.build();
//
httpGet.setConfig(config);
CloseableHttpResponse response = null;
try{
response = httpClient.execute(httpGet);
//4. ,
if( response.getStatusLine().getStatusCode() == 200){
HttpEntity httpEntity = response.getEntity();
String content = EntityUtils.toString(httpEntity, "utf8");
System.out.println(content);
}
}catch(IOException e){
e.printStackTrace();
}finally {
// response
try{
response.close();
}catch(IOException e){
e.printStackTrace();
}
try{
httpClient.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}