Get请求带参数
public class HttpGetParamTest {
public static void main(String[] args) throws Exception {
// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 设置请求地址是: http://yun.itheima.com/search?keys=Java
// 创建URIBuilder
URIBuilder uriBuilder = new URIBuilder("http://yun.itheima.com/search");
// 设置参数
uriBuilder.setParameter("keys", "Java");
// 创建HttpGet对象, 设置url访问地址
HttpGet httpGet = new HttpGet(uriBuilder.build());
System.out.println("发起请求的信息: " + httpGet);
CloseableHttpResponse response = null;
try {
// 使用 HttpClient 发起请求, 获取 response
response = httpClient.execute(httpGet);
// 解析响应
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "utf8");
}
} catch(IOException e) {
e.printStackTrace();
} finally {
// 关闭 response
try {
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Post请求带参数
public class HttpPostParamTest {
public static void main(String[] args) throws Exception {
// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建HttpPost对象, 设置url访问地址
HttpPost httpPost = new HttpPost("http://yun.itheima.com/search");
// 声明List集合, 封装表单中的参数
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("keys", "Java"));
// 创建表单的Entity对象, 第一个参数就是封装好的表单数据, 第二个参数就是编码
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf8");
// 设置表单的Entity对象到 Post 请求中
httpPost.setEntity(formEntity);
CloseableHttpResponse response = null;
try {
// 使用 HttpClient 发起请求, 获取 response
response = httpClient.execute(httpPost);
// 解析响应
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content.length());
}
} catch(IOException e) {
e.printStackTrace();
} finally {
// 关闭 response
try {
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Jsoup
<!--Jsoup-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<!--操作字符串-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4
//解析需要的东西,一般解析httpClient执行得到的response.getEntity的字符串
Document doc = Jsoup.parse(“字符串|文件|url”);
//使用方法,形同jquery
// el#id: 元素+ID,比如: h3#city_bj
Element element = doc.select("h3#city_bj").attr();