使用Apache的HttpClient发送http请求的过程
1.第一步,获取一个CloseableHttpClient对象,用来发送httppost和httpget请求
2.第二步,创建一个HttpPost或者HttpGet对象,用来设置请求的url,参数,post请求的body实体等
3.第三步,使用CloseableHttpClient对象的execute将请求发送出去,并得到一个CloseableHttpResponse对象
4.第四步,使用ClooseableHttpResponse对象的方法来处理响应
发送无参数的Http请求
public void doHttpGet(){
CloseableHttpClient httpClient= HttpClientBuilder.create().build();
HttpGet httpGet=new HttpGet("https://www.baidu.com/");
CloseableHttpResponse response=null;
//使用httpclient发送get请求
try{
response=httpClient.execute(httpGet);
HttpEntity entity=response.getEntity();
System.out.println("响应长度 "+entity.getContentLength());
System.out.println("响应状态 "+response.getStatusLine());
System.out.println("响应内容 "+ EntityUtils.toString(entity));
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
httpClient.close();
}catch (IOException e){
e.printStackTrace();
}
try {
response.close();
}catch (IOException e){
e.printStackTrace();
}
}
get请求带查询参数 query param
直接在url中拼接参数
public void doGetWithParam(){
CloseableHttpClient httpClient=HttpClientBuilder.create().build();
String url="http://httpbin.org/get"+"?"+"key1=value1";
HttpGet httpGet=new HttpGet(url);
CloseableHttpResponse response=null;
try {
response = httpClient.execute(httpGet);
System.out.println(httpGet.getURI());
HttpEntity httpEntity=response.getEntity();
System.out.println(EntityUtils.toString(httpEntity));
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
httpClient.close();
}catch (IOException e){
e.printStackTrace();
}
try{
response.close();
}catch (IOException e){
e.printStackTrace();
}
}
get请求,使用URI构建url和查询参数 query param
public void doGetWithURI(){
CloseableHttpClient httpClient=HttpClientBuilder.create().build();
CloseableHttpResponse response=null;
//查询参数
List<NameValuePair> pairs=new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1","value"));
pairs.add(new BasicNameValuePair("key2","value2"));
//使用URIBuilder拼接URI
URI uri=null;
try {
uri = new URIBuilder().setScheme("http").setHost("httpbin.org").setPath("get").addParameters(pairs).build();
}catch (URISyntaxException e){
e.printStackTrace();
}
HttpGet httpGet=new HttpGet(uri);
try{
response=httpClient.execute(httpGet);
System.out.println(EntityUtils.toString(response.getEntity()));
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
post请求,请求的body是表单格式
public void postFormParam(){
//发送的body为表单格式
CloseableHttpClient httpClient=HttpClientBuilder.create().build();
CloseableHttpResponse response=null;
HttpPost httpPost=new HttpPost("http://httpbin.org/post");
//构造表单参数
List<NameValuePair> pairs=new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1","value1"));
pairs.add(new BasicNameValuePair("key2","value2"));
UrlEncodedFormEntity requestEntity=null;
try {
requestEntity = new UrlEncodedFormEntity(pairs);
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
httpPost.setEntity(requestEntity);
try{
response=httpClient.execute(httpPost);
System.out.println(EntityUtils.toString(response.getEntity()));
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
注意:UrlEncodedFormEntity可以设置编码格式
UrlEncodedFormEntity(List<? extends org.apache.http.NameValuePair> parameters, String charset)
Constructs a new UrlEncodedFormEntity with the list of parameters in the specified encoding.
post请求,body是json格式,使用StringEntity来构造body
public void postJsonParam(){
CloseableHttpClient httpClient=HttpClientBuilder.create().build();
CloseableHttpResponse response=null;
HttpPost httpPost=new HttpPost("https://api.github.com/some/endpoint");
Map<String,String> jsonMap=new HashMap<String, String>();
jsonMap.put("key1","value1");
jsonMap.put("key2","value2");
String jsonString= JSON.toJSONString(jsonMap);
try {
StringEntity requestEntity=new StringEntity(jsonString);
requestEntity.setContentType(ContentType.APPLICATION_JSON.toString());
httpPost.setEntity(requestEntity);
response=httpClient.execute(httpPost);
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println(httpPost.getURI());
System.out.println(EntityUtils.toString(httpPost.getEntity()));
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
响应的处理
响应一般是json格式
1.使用JSON.parseObject,将json串转换为对象
public void doHttpGet(){
CloseableHttpClient httpClient= HttpClientBuilder.create().build();
HttpGet httpGet=new HttpGet("http://localhost:8080/queryOne");
CloseableHttpResponse response=null;
//使用httpclient发送get请求
try{
response=httpClient.execute(httpGet);
HttpEntity entity=response.getEntity();
//将JSON响应和一个实体类对应
Account account=JSON.parseObject(EntityUtils.toString(entity),Account.class);
System.out.println("Account "+account.toString());
System.out.println(account instanceof Account);
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
httpClient.close();
}catch (IOException e){
e.printStackTrace();
}
try {
response.close();
}catch (IOException e){
e.printStackTrace();
}
}
2.响应是一个list的json,使用JSON.parseArray
public void doHttpGet(){
CloseableHttpClient httpClient= HttpClientBuilder.create().build();
HttpGet httpGet=new HttpGet("http://localhost:8080/queryAll");
CloseableHttpResponse response=null;
//使用httpclient发送get请求
try{
response=httpClient.execute(httpGet);
HttpEntity entity=response.getEntity();
//将JSON响应和一个实体类对应
List<Account> accounts=JSON.parseArray(EntityUtils.toString(entity),Account.class);
System.out.println("Account "+accounts.toString());
System.out.println(accounts instanceof List);
System.out.println(accounts.get(0).getId());
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
httpClient.close();
}catch (IOException e){
e.printStackTrace();
}
try {
response.close();
}catch (IOException e){
e.printStackTrace();
}
}