一个手写的 http client
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的更多相关文章
- 最新用WPF为触摸屏写了一个手写程序,双格输入的
原文:最新用WPF为触摸屏写了一个手写程序,双格输入的 双格输入可以提高手写速度,当前字写完以后可以自动识别提交,写下一个字.这样比单格手写速度提高一倍.特别适合触摸屏程序使用 界面如下: 程序如下: ...
- 一个手写的Vue放大镜,复制即可使用
一个手写的vue放大镜 组件使用less,请确保已安装loader 本组件为放大镜组件,传参列表为: width: 必传,设置放大镜的宽高(正方形),放大区域等同,放大倍数为2倍 picList:必传 ...
- 依据ECMA规范,手写一个bind函数
Function.prototype.bind 函数,参见ECMA规范地址 如题,这次来实现一个boundFunction函数,不挂载在Function.prototype上,而是一个单独声明的函数. ...
- 浅析MyBatis(二):手写一个自己的MyBatis简单框架
在上一篇文章中,我们由一个快速案例剖析了 MyBatis 的整体架构与整体运行流程,在本篇文章中笔者会根据 MyBatis 的运行流程手写一个自定义 MyBatis 简单框架,在实践中加深对 MyBa ...
- 【OpenCV】opencv3.0中的SVM训练 mnist 手写字体识别
前言: SVM(支持向量机)一种训练分类器的学习方法 mnist 是一个手写字体图像数据库,训练样本有60000个,测试样本有10000个 LibSVM 一个常用的SVM框架 OpenCV3.0 中的 ...
- MNIST手写数字数据库
手写数字库很容易建立,但是总会很浪费时间.Google实验室的Corinna Cortes和纽约大学柯朗研究所的Yann LeCun建有一个手写数字数据库,训练库有60,000张手写数字图像,测试库有 ...
- 10分钟搞懂Tensorflow 逻辑回归实现手写识别
1. Tensorflow 逻辑回归实现手写识别 1.1. 逻辑回归原理 1.1.1. 逻辑回归 1.1.2. 损失函数 1.2. 实例:手写识别系统 1.1. 逻辑回归原理 1.1.1. 逻辑回归 ...
- 深度学习之 mnist 手写数字识别
深度学习之 mnist 手写数字识别 开始学习深度学习,先来一个手写数字的程序 import numpy as np import os import codecs import torch from ...
- AI应用开发实战 - 手写算式计算器
扩展手写数字识别应用 识别并计算简单手写数学表达式 主要知识点 了解MNIST数据集 了解如何扩展数据集 实现手写算式计算器 简介 本文将介绍一例支持识别手写数学表达式并对其进行计算的人工智能应用的开 ...
随机推荐
- 3dmax多个版本软件的安装包以及安装教程
这个文档具体出自哪里,我也是记不得了,需要的看下,链接如果是失效,那我也无能为力了. 免费分享,链接永久有效 2014版3D MAX链接:http://pan.baidu.com/s/1nuFr7Xv ...
- [Visual Studio] 自定义类模板
1.找到vs2015/vs2012/vs2017的安装目录下:Common7\IDE\ItemTemplates\CSharp\Code\2052\Class 2.打开Class.cs文件 using ...
- SpringMVC类型转换、数据绑定详解
public String method(Integer num, Date birth) { ... } Http请求传递的数据都是字符串String类型的,上面这个方法在Controller中定义 ...
- Exception的ToString()方法究竟返回的是什么
最近项目上线后遇到exception没有堆栈信息.所以跟踪一下 源码,其中主要的code如下: // Returns the stack trace as a string. If no stack ...
- STM32 F103 F407 F429 F767对比图
- centos7下opencv3的安装
前面说过opencv和python模块的安装,版本是基于opencv 2的,这篇主要说一下opencv3的安装过程以及相关依赖问题的解决,同样opencv3的安装包在官网上可以下载,最新稳定版是3.3 ...
- 【SQL 代码】SQL复制数据表及表结构
select * into 目标表名 from 源表名 from 源表名 以上两句都是将'源表'的数据插入到'目标表',但两句又有区别的: 第一句(select into from)要求目标表不存在, ...
- vmware中如何检查cpu的使用状况-一个考题引发的思考
来自一个VCP的考题,有点兴趣.可参看: 如何在VMware里使用esxtop? http://thocm.com/a/caozuoxitongzixun/xunihuazonghezixun/VMw ...
- MySQL中的insert ignore into, replace into用法总结
MySQL replace into 有三种形式: 1. replace into tbl_name(col_name, ...) values(...) 2. replace into tbl_na ...
- plsql 操纵表数据的2种方式
1.情景展示 在plsql中,对表的操作(增.删.改.查),其实有2种方式,只是很多人都只会第一种方式罢了. 2.方式介绍 第1种方式:直接将值与sql写到一起 直接按F8运行即可. 第2种方式: ...