本篇主要说明一下get请求和post请求
get请求分为有参数和无参数
get请求无参数
public static void main(String[] args) {
//使用HttpClient发起请求,获取response
CloseableHttpResponse response=null;
//创建HttpClient对象,相当于创建浏览器
CloseableHttpClient httpClient= HttpClients.createDefault();
//创建HttpGet对象,设置URL访问地址
HttpGet httpGet=new HttpGet("https://data.sh.gov.cn/");
try {
//使用HttpClient发起请求,获取response
response=httpClient.execute(httpGet);
//判断状态码
if(response.getStatusLine().getStatusCode()==200){
//解析响应
String content=EntityUtils.toString(response.getEntity(),"UTF-8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
/* 释放资源 */
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭浏览器
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
get请求有参数,主要使用uriBuilder.setParameter(“keys”,“value”),如果想设置多参数,可以直接在后面追加.setParameter(“keys”,“value”)…
public static void main(String[] args) throws Exception {
//使用HttpClient发起请求,获取response
CloseableHttpResponse response=null;
//创建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");
//设置多参数
//uriBuilder.setParameter("keys","Java").setParameter("","");
//创建HttpGet对象,设置URL访问地址
HttpGet httpGet=new HttpGet(uriBuilder.build());
System.out.println("发起请求的信息"+httpGet);
try {
//使用HttpClient发起请求,获取response
response=httpClient.execute(httpGet);
//判断状态码
if(response.getStatusLine().getStatusCode()==200){
//解析响应
String content= EntityUtils.toString(response.getEntity(),"UTF-8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
/* 释放资源 */
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭浏览器
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
post请求也分为有参数和无参数
post请求无参数和get请求无参数很相似,只需将HttpGet改为HttpPost即可
public static void main(String[] args) {
//使用HttpClient发起请求,获取response
CloseableHttpResponse response = null;
//创建HttpClient对象,相当于创建浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建HttpGet对象,设置URL访问地址
HttpPost httpPost = new HttpPost("http://data.sh.gov.cn/");
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(httpPost);
//判断状态码
if (response.getStatusLine().getStatusCode() == 200) {
//解析响应
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
/* 释放资源 */
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭浏览器
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
post请求有参数,需要走list集合,将表单的数据进行一个简单封装再使用即可
public static void main(String[] args) throws Exception {
//使用HttpClient发起请求,获取response
CloseableHttpResponse response = null;
//创建HttpClient对象,相当于创建浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建HttpPost对象,设置URL访问地址
HttpPost httpPost = new HttpPost(“http://yun.itheima.com/search”);
//声明List集合,封装表单中的参数
List<NameValuePair> params=new ArrayList<NameValuePair>();
//设置请求地址是http://yun.itheima.com/search?keys=Java
params.add(new BasicNameValuePair("keys","Java"));
//创建表单的Entity对象,参数1为封装好的表单数据,参数2为编码
UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(params,"UTF-8");
//设置表单的Entity对象到Post请求中
httpPost.setEntity(formEntity);
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(httpPost);
//判断状态码
if (response.getStatusLine().getStatusCode() == 200) {
//解析响应
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
/* 释放资源 */
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭浏览器
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}