一个手写的 http client
2015-07-16 10:30
489 查看
public class HTTPClient { public static final String GET = "GET"; public static final String POST = "POST"; public static final String PUT = "PUT"; public static final String DELETE = "DELETE"; public static String getData(String path) throws Exception { String responseData = visitWithoutParam(path, GET); return responseData; } public static String postData(String path, String data) throws Exception { String responseData = visitWithParam(path, POST, data); return responseData; } public static String putData(String path, String data) throws Exception { return "To do put data"; } public static String deleteData(String path) throws Exception { return "To do delete data"; } public static String visitWithParam(String path, String method, String body) throws Exception{ InputStream inputStream = null; BufferedReader bufferedReader = null; try { URL url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod(method); httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.setRequestProperty("charset", "utf-8"); httpURLConnection.setRequestProperty("Content-Length", Integer.toString(body.getBytes().length)); DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream()); dataOutputStream.writeBytes(body); dataOutputStream.flush(); dataOutputStream.close(); inputStream = httpURLConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line = null; while((line = bufferedReader.readLine()) != null) stringBuilder.append(line); return stringBuilder.toString(); } catch (Exception e) { throw new Exception(e); } finally { // no need to change null actually try { if(bufferedReader != null) bufferedReader.close(); if(inputStream != null) inputStream.close(); } catch (Exception e){ } } } public static String visitWithoutParam(String path, String method) throws Exception { InputStream inputStream = null; BufferedReader bufferedReader = null; URL url; try { url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod(method); inputStream = httpURLConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line = null; while((line = bufferedReader.readLine()) != null) stringBuilder.append(line); return stringBuilder.toString(); } catch (Exception e) { throw new Exception(e); } finally { try { if(bufferedReader != null) bufferedReader.close(); if(inputStream != null) inputStream.close(); } catch (Exception e) { } } } }
自己很久以前写过的一段代码,当时忘记了 apache.httpclient 这个东西,结果又重新造了个轮子
相关文章推荐
- 【Linux 内核网络协议栈源码剖析】sendto 函数剖析
- iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
- 使用mina解析http协议的使用
- iOS开发工具-网络封包分析工具Charles
- 【Android小练手】安卓端实时获取HttpUrl数据
- dhroid - NetJSONAdapter 网络化的adapter
- 2014中国网络性能报告
- dhroid - DhNet 网络http工具
- 【源码剖析】tinyhttpd —— C 语言实现最简单的 HTTP 服务器
- HTTP协议详解
- TCP同步和异步连接_学习笔记
- FPGA实现网络通信时的网络字节序问题
- 巧用Android网络通信技术,在网络上直接传输对象
- 基于java的http服务器
- 微软“小冰”识狗与人工神经网络(I)
- HTTP 笔记与总结(6)referer 头与防盗链
- 企业社交网络平台与传统OA办公系统之间的较量
- 第一部分:HTTP协议之请求和响应
- BZOJ 1475: 方格取数( 网络流 )
- java网络编程(2)——UDP与TCP