get请求
get无参请求
public static void main(String[] args) {
//创建htppClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建HttpGet对象,设置url访问地址
HttpGet HttpGet = new HttpGet("http://wwww.wuquejs.cn");
CloseableHttpResponse response = null;
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(HttpGet);
//解析响应
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
//打印输出请求得到的内容
System.out.println( EntityUtils.toString(entity));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
get有参请求
public static void main(String[] args) throws Exception {
//创建htppClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建URIBuilder
URIBuilder uriBuilder = new URIBuilder("Http://www.wuquejs.cn");
//设置参数,以键值对的方式存入uriBuilder
uriBuilder.setParameter("keys", "java");
//创建HttpGet对象,设置url访问地址
HttpGet httpGet = new HttpGet(uriBuilder.build());
//打印输出完成的请求url
System.out.println("发起请求的信息" + httpGet);
CloseableHttpResponse response = null;
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(httpGet);
//解析响应
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
//打印输出请求得到的内容
System.out.println( EntityUtils.toString(entity));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭response
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
get请求的有参可以直接把请求的参数拼接到url后面
post请求
post无参请求
public static void main(String[] args) {
//创建htppClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建HttpPost对象,设置url访问地址
HttpPost HttpPost = new HttpPost("http://www.wuquejs.cn");
CloseableHttpResponse response = null;
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(HttpPost);
//解析响应
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
//打印输出请求得到的内容
System.out.println( EntityUtils.toString(entity));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭response
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
post有参请求
public static void main(String[] args) throws Exception {
//创建htppClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建HttpGet对象,设置url访问地址
HttpPost httpPost = new HttpPost("http://www.wuquejs.cn");
//生命List集合,封装表单中的参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
//设置请求地址:http://www.wuquejs.cn/search?keys=Java
params.add(new BasicNameValuePair("keys", "java"));
//创建表单的Entity对象
//第一个参数就是封装好的表单数据,第二个参数就是编码
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf-8");
//设置表单的Entity对象到Post请求中
httpPost.setEntity(formEntity);
CloseableHttpResponse response = null;
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(httpPost);
//解析响应
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
//打印输出请求得到的内容
System.out.println( EntityUtils.toString(entity));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭response
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1 条评论
学习一下,大爱。