java根据isbn编码获取书籍信息(附赠http url的util包)
2017-09-11 16:41
453 查看
controller代码:
自定义httpUtil包:
效果图:
/** * 测试 * * */ @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 * @retur 4000 n 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; } }
效果图:
相关文章推荐
- C# HttpWebRequest 绝技 根据URL地址获取网页信息
- C# HttpWebRequest 绝技 根据URL地址获取网页信息
- 调用豆瓣的接口根据ISBN获取书籍信息
- java--根据URL获取信息
- 【JAVA】通过ISBN一键获取书籍信息
- java根据网页URL获取正文信息,并调整正文格式为段落显示---(两种方式)
- C# HttpWebRequest 绝技 根据URL地址获取网页信息
- Java中路径的获取总结以及URL和URI的区别_Java大本营_职场_西祠胡同 http://www.xici.net/d174934654.htm
- 以HttpGet方法获取URL数据并转换成指定编码格式的字符串
- 根据URL地址获取信息
- VC 获取托盘图标信息 . (2012-12-11 20:33:58) 标签: it 分类: 基础知识 //本例以获取程序托盘图标位置为例 //根据需要还可以获取不少信息 参考:http://www.
- java脚本开发根据客户IP获取IP的具体地理位置信息
- JAVA根据IP地址获取详细的地域信息
- JAVA根据IP地址获取详细的地域信息
- Ajax特效 根据邮政编码获取地区信息 之一
- java根据ip地址获取详细地域信息
- 根据ISBN获取豆瓣API提供的图书信息
- java根据ip地址获取详细地域信息
- 用JAVA中的URL获取网页相关信息
- 关于获取java获取http响应头信息