java根据isbn编码获取书籍信息(附赠http url的util包)

controller代码:

    /** 
     * 测试
     * 
     * */
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public JsonResult test() {
         String url="https://api.douban.com/v2/book/isbn/:"+"9787115417305";  
         String result = HttpUtil.post(url, "", "GBK");
         String url1="http://jisuisbn.market.alicloudapi.com/isbn/query?isbn="+"9787212058937";  
         String result1 = HttpUtil.get(url1, "GBK");
         Map<String, Object> map = new HashMap<>();
         map.put("result", result);
         map.put("result1", result1);
         return new JsonResult(ResultCode.SUCCESS, "检测成功",map);
    }

自定义httpUtil包:

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

    /**
     * get
     *
     * @param url
     *            String
     * @param charset
     *            String
     * @return String
     */
    public static String get(String url, String charset) {
        if (charset == null) {
            charset = HTTP.UTF_8;
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try{
            HttpGet httpget = new HttpGet(url);
            CloseableHttpResponse response = httpClient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, charset);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (response != null){
                    response.close();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 关闭连接,释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * getForJisu
     *
     * @param url
     *            String
     * @param charset
     *            String
     * @return String
     */
    public static String getForJisu(String url, String charset) {
        if (charset == null) {
            charset = HTTP.UTF_8;
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try{
            HttpGet httpget = new HttpGet(url);
            httpget.addHeader("key", "value");
            CloseableHttpResponse response = httpClient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, charset);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (response != null){
                    response.close();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 关闭连接,释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * postForString
     *
     * @param url
     *            String
     * @param content
     *            content
     * @param charset
     *            String
     * @return String
     */
    public static String post(String url, String content, String charset) {
        if (charset == null) {
            charset = HTTP.UTF_8;
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(url);
            if (content != null) {
                httpPost.setEntity(new StringEntity(content, charset));
            }
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, charset);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (response != null){
                    response.close();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 关闭连接,释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * postForMap
     * 
     * @param url
     *            String
     * @param map
     *            HashMap 提交表单的键值对
     * @param charset
     *            String
     * @return String
     */
    public static String post(String url, HashMap<String, String> map, String charset) {
        if (charset == null) {
            charset = HTTP.UTF_8;
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(url);
            List<NameValuePair> nvps = new ArrayList<>();
            if (map != null) {
                Iterator it = map.keySet().iterator();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    nvps.add(new BasicNameValuePair(key, map.get(key)));
                }
            }
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, charset));
            // 设置header信息   指定报文头【Content-type】、【User-Agent】
            //httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
            //httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            // 执行请求操作,并拿到结果(同步阻塞)
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, charset);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (response != null){
                    response.close();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 关闭连接,释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

效果图:
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值