【Http】httputils实现get/post请求
2015-10-26 16:35
543 查看
import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; import java.io.InputStream; /** * Created on 2015/7/28. * * @author * @version 1.0 */ public abstract class HttpUtils { private static CloseableHttpClient httpClient = HttpClients.createDefault(); private static ObjectMapper mapper = new ObjectMapper(); public static String get(String url) { HttpGet httpGet = new HttpGet(url); try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { HttpEntity entity = httpResponse.getEntity(); InputStream inputStream = entity.getContent(); String result = IOUtils.toString(inputStream, "UTF-8"); inputStream.close(); return result; } catch (IOException e) { throw new RuntimeException(e); } } public static <T> T get(String url, Class<T> clazz) { String result = get(url); if (StringUtils.isNotBlank(result)) { try { return mapper.readValue(result, clazz); } catch (Exception e) { throw new RuntimeException(e); } } return null; } }
相关文章推荐
- socket网络编程复习笔记(二):套接字基本原理(下)
- 获取手机版本,手机型号,系统版本,手机设备唯一序列号,手机应用版本,手机网络类型的总结
- DSAPI多功能组件编程应用-实时统计-CPU/内存/硬盘/网络
- [Servlet]HttpServletRequest的请求参数、标头、编码
- web服务器测试工具Gulu——HTTP接口测试工具
- php模拟HTTP协议请求
- IIS7.5上的REST服务的Put操作发生HTTP Error 405.0 - Method Not Allowed 解决方法
- tcp/udp的区别
- tcpdump
- iOS 网络编程模式总结
- java常用设计模式(转载http://www.cnblogs.com/hnrainll/archive/2011/12/29/2305582.html)
- 网络开发---UIWebView
- 在电视端网络视频播放加载一个视频预览的方法(电视基于安卓系统)
- 一种在本地搭载一个服务器的工具和方法,Android开发测试网络视频可以用
- ImageLoader下载图片
- Toast 数据加载,内容加载,网络图片加载提示
- JVM通过代理服务器连接网络的参数设置
- php模拟HTTP响应
- http和socket之长连接和短连接区别
- 网络编程 笔记(十)多播与广播