httpclient4例子
参考:http://hc.apache.org/httpclient-3.x/tutorial.html
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.protocol.HTTP;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL; @Component
public class HttpClientUtil {
private static CloseableHttpClient httpClient = HttpClients.createDefault(); private static String ADDRESS; public static String getAuthorization() {
String url = "http://" + ADDRESS + "/login";
String data = "{\"username\": \"username\",\"password\": \"password\"}";
HttpURLConnection connection=null;
try {
URL server = new URL(url);
connection = (HttpURLConnection)server.openConnection();
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type","application/json;charset=utf-8");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(5*1000);
connection.setReadTimeout(10*1000);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(data.toString().getBytes("UTF-8"));
wr.flush ();
wr.close (); String Authorization =connection.getHeaderField("Authorization");
return Authorization;
} catch (Exception e){
e.printStackTrace();
} finally{
if(connection != null ){
connection.disconnect();
}
}
return null;
} public static String post(String url, String body) {
String auth = getAuthorization();
if (auth == null) {
return null;
} HttpPost httpPost = new HttpPost("http://" + ADDRESS + url);
httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8");
httpPost.setHeader("Accept", "application/json;charset=UTF-8");
httpPost.setHeader("Authorization", auth); CloseableHttpResponse response2 = null;
try {
StringEntity entity = new StringEntity(body, "utf-8");
System.out.println("request = " + body); httpPost.setEntity(entity); response2 = httpClient.execute(httpPost);
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
String responseStr = new String(IOUtils.toByteArray(entity2.getContent()));
System.out.println("response = " + responseStr); return responseStr;
} catch(Exception e) {
e.printStackTrace();
return null;
} finally {
if (response2 != null) {
try {
response2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} @Value("${interface.address}")
public void setAddress(String address) {
ADDRESS = address;
}
}
httpclient4例子的更多相关文章
- httpclient4 模拟访问网页 模拟登录 简单例子
JAVA后台模拟登录一个网站,获得一定权限后进一步操作. 所用的工具: Apache HttpComponents client 4.3版本 以下为代码: import org.apache.http ...
- 用Java开发gRPC服务的例子分析
本文的代码例子来自:https://github.com/grpc/grpc-java 定义服务 这一步与其他语言完全一样,需要定义gRPC的服务.方法.request和response的类型. 完 ...
- HttpClient4的使用,模拟浏览器登陆新浪微博,发表微博和文字+图片微博
HttpClient4,最原始的需求就是使用其来模拟浏览器想服务器发起http请求,当然,他的功能不止于此,但是我需要的就是这个功能而已,jdk也有其自带的类似的api:UrlConnection,效 ...
- HttpClient4.5.2调用示例(转载+原创)
操作HttpClient时的一个工具类,使用是HttpClient4.5.2 package com.xxxx.charactercheck.utils; import java.io.File; i ...
- HTTP摘要认证原理以及HttpClient4.3实现
基本认证便捷灵活,但极不安全.用户名和密码都是以明文形式传送的,也没有采取任何措施防止对报文的篡改.安全使用基本认证的唯一方式就是将其与 SSL 配合使用. 摘要认证是另一种HTTP认证协议,它试图修 ...
- httpclient4.5 的一些细节
本文转自:http://mercymessi.iteye.com/blog/2250161 httpclient是Apache下的一个用于执行http网络访问的一个工具包. 大致流程:新建一个http ...
- SQLServer地址搜索性能优化例子
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...
- C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)
第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...
- REGEX例子
作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...
随机推荐
- chrome和Firefox对p标签中单词换行的渲染(强制换行)
谷歌和火狐对p标签单词的渲染: 今天在p标签展示url链接中,由于有几个下划线拼接的单词特别长, 所以总有那么几行老是超出p标签的范围,然后设置了强制 换行,才得以解决. word-wrap : br ...
- R语言学习——输入与输出
导入数据: grades<-read.table("D:/ProgramData/test1.txt",sep="\t") 求均值:mean() 求方差: ...
- HDU 2548 A strange lift
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 网络性能测试工具iperf详细使用图文教程(转)
Iperf是一个网络性能测试工具.Iperf可以测试TCP和UDP带宽质量.Iperf可以测量最大TCP带宽,具有多种参数和UDP特性.Iperf可以报告带宽,延迟抖动和数据包丢失.利用Iperf这一 ...
- maven package 命令报:-source1.3 中不支持注释错误
在使用maven 打包或者编译时报:-source1.3 中不支持注释错误解决方案如下: <build> <plugins> <plugin> < ...
- sklearn.naive_bayes中几种朴素贝叶斯分类器
区别: 几种朴素贝叶斯分类器的区别在于对于分布的假设,即假设满足的形式. 一.高斯NB 导入 from sklearn.naive_bayes import GaussianNB 假设特征的似然函数满 ...
- python-django-ORM,常用查询方式
介绍django model 的一些常用查询方式 首先是一些文档性的帮助 __exact 精确等于 like ‘aaa’ __iexact 精确等于 忽略大小写 ilike ‘aaa’ __conta ...
- django中的 form 表单操作
form组件 1. 能做什么事? 1. 能生成HTML代码 input框 2. 可以校验数据 3. 保留输入的数据 4. 有错误的提示 1. 定义 from django ...
- python中的模块及包及软件目录结构规范
知识内容: 1.模块的定义与分类 2.模块的导入 3.模块与包 4.不同目录下的模块调用 一.模块的定义与分类 1.什么是模块 模块就是实现了某个功能的代码集合,模块是由一大堆代码构成的 类似于函数式 ...
- python知识点, float不能用 != 判断
python知识点链接:https://github.com/taizilongxu/interview_python 搜索:python最佳事件 书单:http://lucida.me/blog/d ...