参考: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. vue 感觉很好的渲染模式

    <ul v-if="todos.length"> <li v-for="todo in todos"> {{ todo }} </ ...

  2. Bezier画线算法

    编译器:VS2013 描述:Bezier画线是利用导数相同拼接曲线,使曲线十分光滑,而不是随意拼接观赏性很差 主函数段 #include "stdafx.h" #include&l ...

  3. 微信小程序之for循环

    在微信小程序中也有for循环,用于进行列表渲染. 官方实例 打开微信开发者文档,在框架部分的视图层-->wxml-->列表渲染中可以看到官方给出的for循环实例,在实例中 可以看到下面相关 ...

  4. 针对IE6浏览器下,zoom:1的问题

    一.css代码如下: .message .con .word {font-size:14px;color:#333333; border-radius:3px; padding:10px;border ...

  5. docker 基础操作

    1. 安装docker 系统centos 7.2 yum -y install docker-io service docker start 安装完毕后执行 docker version 或者dock ...

  6. Linux下的压缩(zip)解压(unzip)缩命令

      .zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip命令 unzip -o ...

  7. 关于安卓苹果手机安装证书抓https的关键步骤

    苹果有关键步骤!!!

  8. linux开机服务自启

    有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务,主要用三种方式进行这一操作: ln -s             在/etc/rc.d/rc*.d目录中建立/etc/init.d/ ...

  9. servlet的讲解

    https://www.ibm.com/developerworks/cn/java/j-lo-servlet/

  10. C++多线程同步之事件(Event)

    原文链接:http://blog.csdn.net/olansefengye1/article/details/53291074 一.事件(Event)原理解析 1.线程同步Event,主要用于线程间 ...