参考: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例子的更多相关文章

  1. httpclient4 模拟访问网页 模拟登录 简单例子

    JAVA后台模拟登录一个网站,获得一定权限后进一步操作. 所用的工具: Apache HttpComponents client 4.3版本 以下为代码: import org.apache.http ...

  2. 用Java开发gRPC服务的例子分析

    本文的代码例子来自:https://github.com/grpc/grpc-java  定义服务 这一步与其他语言完全一样,需要定义gRPC的服务.方法.request和response的类型. 完 ...

  3. HttpClient4的使用,模拟浏览器登陆新浪微博,发表微博和文字+图片微博

    HttpClient4,最原始的需求就是使用其来模拟浏览器想服务器发起http请求,当然,他的功能不止于此,但是我需要的就是这个功能而已,jdk也有其自带的类似的api:UrlConnection,效 ...

  4. HttpClient4.5.2调用示例(转载+原创)

    操作HttpClient时的一个工具类,使用是HttpClient4.5.2 package com.xxxx.charactercheck.utils; import java.io.File; i ...

  5. HTTP摘要认证原理以及HttpClient4.3实现

    基本认证便捷灵活,但极不安全.用户名和密码都是以明文形式传送的,也没有采取任何措施防止对报文的篡改.安全使用基本认证的唯一方式就是将其与 SSL 配合使用. 摘要认证是另一种HTTP认证协议,它试图修 ...

  6. httpclient4.5 的一些细节

    本文转自:http://mercymessi.iteye.com/blog/2250161 httpclient是Apache下的一个用于执行http网络访问的一个工具包. 大致流程:新建一个http ...

  7. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  8. C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)

    第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...

  9. REGEX例子

    作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...

随机推荐

  1. vscode中使用Experimental Decorators报错

    在vscode中使用es7的新语法decorator会报错,如图: 这是错误来自与vscode的JS support,只要在项目根目录下创建一个jsconfig.json文件,添加如下内容: { &q ...

  2. 求交错序列前N项和(15 分)

    7-2 求交错序列前N项和(15 分) 本题要求编写程序,计算交错序列 1-2/3+3/5-4/7+5/9-6/11+... 的前N项之和. 输入格式: 输入在一行中给出一个正整数N. 输出格式: 在 ...

  3. 杂项:node.js

    ylbtech-杂项:node.js Node.js是一个Javascript运行环境(runtime),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.No ...

  4. HTML5: input:file上传类型控制

    ylbtech-HTML5: input:file上传类型控制   1. 一.input:file 属性返回顶部 一.input:file属性 属性值有以下几个比较常用: accept:表示可以选择的 ...

  5. Jmeter接口压测

    对于各个组件的使用,建议参考官方文档 1. Jmeter参数化,从txt文件读取. 1.txt

  6. ssh的应用和vnc连接桌面

    什么是ssh? SSH是一种网络协议,用于计算机之间的加密登录.如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会泄露.SSH之 ...

  7. CA证书扫盲,https讲解

    很多关于CA证书的讲解. 1.什么是CA证书. 看过一些博客,写的比较形象具体. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 B 公司的所有 ...

  8. PostgreSQL下安装pg_stat_statements

    一.安装过程如下:进入postgreSQL安装包的contrib/pg_stat_statements目录,执行编译和安装动作:用root用户 make && make install ...

  9. Web of Science数据库中文献相关信息下载与保存

    1. Web of Science 数据库(https://apps.webofknowledge.com/): a. 所在网络必须由访问 该网站的权限. b.建议使用web of Science的核 ...

  10. 经典算法 KMP算法详解

    内容: 1.问题引入 2.暴力求解方法 3.优化方法 4.KMP算法 1.问题引入 原始问题: 对于一个字符串 str (长度为N)和另一个字符串 match (长度为M),如果 match 是 st ...