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命令,在这里只是给出了另一种方式).这个程序像下 ...
随机推荐
- Zabbix二次开发_03api列表_中文版
基于ZABBIX 3.0 https://www.zabbix.com/documentation/3.0/manual/api/reference 参考方法 本节提供了的zabbix提供的功能的概述 ...
- sikuli 安装
1 简介 官方介绍: Sikuli脚本自动化,你在屏幕上看到的任何东西.它使用图像识别,识别和控制GUI组件.这是有用的,当有一个GUI的内部或源代码的访问是不容易的. Sikuli(在墨西哥 ...
- [转]my97 datepicker IE9+ 故障修复方法
转自:http://blog.csdn.net/xuwj1984/article/details/38733483 问题1:my97 datepicker 不能弹出日期下拉框. 解决方法: 1.下载最 ...
- SQL 字段查找
select [name] from sysobjects where [id] in (select [id] from syscolumns where [name]='a1') SQL 2005 ...
- 解决Ubuntu下使用命令行subl 打开Sublime text3无法输入中文的问题
cd /opt/sublime_text/ sudo vim sub-fcitx.c 新建文件sub-fcitx.c,建议放在Sublime Text的所在目录下,将下面的代码复制进去 ,参考: ht ...
- Ubuntu16.04LTS中使用 apt-get install 出现错误 Could not get lock /var/lib/dpkg/lock 的解决方案
背景 近期,在Ubuntu 16.04 LTS 的操作系统中,安装MySQL-python的时候出现缺少依赖包的情况: 当使用命令 # sudo apt-get install xxx 安装依赖包的时 ...
- jQuery 的noConflict()的使用.
我们项目现在需要用到两个js库.一个是jQuery库,还有一个是我们自己开发的轻量级的gys.js库. 而gys库对外提供的接口也是$符号.和jQuery库是一样的,这个时候,两个库就会发生冲突了,我 ...
- TensorFlow系列专题(一):机器学习基础
- Python:23种Pandas核心操作
Pandas 是一个 Python 软件库,它提供了大量能使我们快速便捷地处理数据的函数和方法.一般而言,Pandas 是使 Python 成为强大而高效的数据分析环境的重要因素之一.在本文中,作者从 ...
- FBV和CBV区别
FBV和CBV区别 def dispatch(self, request, *args, **kwargs): # 做分发的 if request.meth ...