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 这个东西,结果又重新造了个轮子  

一个手写的 http client的更多相关文章

  1. 最新用WPF为触摸屏写了一个手写程序,双格输入的

    原文:最新用WPF为触摸屏写了一个手写程序,双格输入的 双格输入可以提高手写速度,当前字写完以后可以自动识别提交,写下一个字.这样比单格手写速度提高一倍.特别适合触摸屏程序使用 界面如下: 程序如下: ...

  2. 一个手写的Vue放大镜,复制即可使用

    一个手写的vue放大镜 组件使用less,请确保已安装loader 本组件为放大镜组件,传参列表为: width: 必传,设置放大镜的宽高(正方形),放大区域等同,放大倍数为2倍 picList:必传 ...

  3. 依据ECMA规范,手写一个bind函数

    Function.prototype.bind 函数,参见ECMA规范地址 如题,这次来实现一个boundFunction函数,不挂载在Function.prototype上,而是一个单独声明的函数. ...

  4. 浅析MyBatis(二):手写一个自己的MyBatis简单框架

    在上一篇文章中,我们由一个快速案例剖析了 MyBatis 的整体架构与整体运行流程,在本篇文章中笔者会根据 MyBatis 的运行流程手写一个自定义 MyBatis 简单框架,在实践中加深对 MyBa ...

  5. 【OpenCV】opencv3.0中的SVM训练 mnist 手写字体识别

    前言: SVM(支持向量机)一种训练分类器的学习方法 mnist 是一个手写字体图像数据库,训练样本有60000个,测试样本有10000个 LibSVM 一个常用的SVM框架 OpenCV3.0 中的 ...

  6. MNIST手写数字数据库

    手写数字库很容易建立,但是总会很浪费时间.Google实验室的Corinna Cortes和纽约大学柯朗研究所的Yann LeCun建有一个手写数字数据库,训练库有60,000张手写数字图像,测试库有 ...

  7. 10分钟搞懂Tensorflow 逻辑回归实现手写识别

    1. Tensorflow 逻辑回归实现手写识别 1.1. 逻辑回归原理 1.1.1. 逻辑回归 1.1.2. 损失函数 1.2. 实例:手写识别系统 1.1. 逻辑回归原理 1.1.1. 逻辑回归 ...

  8. 深度学习之 mnist 手写数字识别

    深度学习之 mnist 手写数字识别 开始学习深度学习,先来一个手写数字的程序 import numpy as np import os import codecs import torch from ...

  9. AI应用开发实战 - 手写算式计算器

    扩展手写数字识别应用 识别并计算简单手写数学表达式 主要知识点 了解MNIST数据集 了解如何扩展数据集 实现手写算式计算器 简介 本文将介绍一例支持识别手写数学表达式并对其进行计算的人工智能应用的开 ...

随机推荐

  1. unity DoTween使用

    先说插件获取,DoTween是一个开源的插件,它的代码托管在Github上[https://github.com/Demigiant/dotween].若只是单纯项目需要是可以去AssetStore获 ...

  2. 解决wsl不能安装z.sh问题

    z.sh是韦大很推崇的类似autojump的bash插件,能够很方便的寻找目录,然而wsl下不能直接使用,解决方法在其github仓库(z)的issue中找到: Reproduce it at Mic ...

  3. react-native布局篇

    原文链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native%E5%B8%83%E5%B1%80/ 一.宽度和 ...

  4. nginx编译安装on mac

    一.编译安装模块 如果是原有包中就有的模块,编译时, ./configure --with-xxx 就可以, 如果是第三方模块,可使用 --add-module, 如果有多个模块的话,只需要多次使用- ...

  5. Web 前端面试题整理(不定时更新)

    重要知识需要系统学习.透彻学习,形成自己的知识链.万不可投机取巧,临时抱佛脚只求面试侥幸混过关是错误的! 面试有几点需注意: 面试题目: 根据你的等级和职位的变化,入门级到专家级,广度和深度都会有所增 ...

  6. jsp使用c:forEach报错 javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext的问题

    今天发现了一个折磨我一天的问题: 在jsp文件中使用 <c:forEach items="${checkResult}" var="item"> & ...

  7. Load 和 DOMContentLoaded 区别

    Load 事件触发代表页面中的 DOM,CSS,JS,图片已经全部加载完毕. DOMContentLoaded 事件触发代表初始的 HTML 被完全加载和解析,不需要等待 CSS,JS,图片加载.

  8. CentOS下防御或减轻DDoS攻击方法(转)

    查看攻击IP 首先使用以下代码,找出攻击者IP netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n 将会得 ...

  9. testng.xml 配置大全

    1.TestNG的运行方式如下: 1 With a testng.xml file 直接run as test suite 2 With ant 使用ant 3 From the command li ...

  10. Hadoop2.2.0分布式安装配置详解[2/3]

    前言 本文主要通过对hadoop2.2.0集群配置的过程加以梳理,所有的步骤都是通过自己实际测试.文档的结构也是根据自己的实际情况而定,同时也会加入自己在实际过程遇到的问题.搭建环境过程不重要,重要点 ...