HttpGet带参请求
package com.shisan.spider;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
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 Spider02 {
// Get带参请求
public static void main(String[] args) {
CloseableHttpClient httpClient= HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
URIBuilder uriBuilder =new URIBuilder("http://www.baidu.com/s");
uriBuilder.setParameter("wd", "Java");
HttpGet httpGet = new HttpGet(uriBuilder.build());
response = httpClient.execute(httpGet);
// 解析数据
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity httpEntity = response.getEntity();
String content = EntityUtils.toString(httpEntity,"utf8");
System.out.println(content.length());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 原教程没有断言
assert response != null;
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Q.E.D.