【HttpClient】一个http_post请求例子
package httpclient.httpclient; import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils; public class HttpClientUtil {
public void testPost() {
//1.创建客户端访问服务器的httpclient对象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//2.定义请求的url
String url = "https://XXX.XXX.com/NC1000001";
//3.以请求的连接地址创建get/post请求对象
HttpPost post = new HttpPost(url);
//如果有header请求,添加header请求
post.addHeader("x-api-key", "XXX");
post.addHeader("x-lang", "en-US");
try {
//4.向服务器发送请求,并获取返回数据
CloseableHttpResponse response = httpClient.execute(post);
//获取返回的所有headers
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
System.out.println(header);
}
//获取返回的状态
int status = response.getStatusLine().getStatusCode();
System.out.println(status);
//获取HttpEntity消息载体对象
HttpEntity entity = response.getEntity();
// EntityUtils中的toString()方法转换服务器的响应数据
String str = EntityUtils.toString(entity, "utf-8");
System.out.println("服务器的响应是:" + str);
//5.关闭连接
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
HttpClientUtil util = new HttpClientUtil();
util.testPost();
}
}
运行结果:
Date: Wed, 06 Sep 2017 09:49:39 GMT
Server: Apache/2.4.7 (Ubuntu)
Cache-Control: no-cache, private
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
Content-Length: 150
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json
200
服务器的响应是:{"id":1000001,"tracking_id":"NC1000001"}
【HttpClient】一个http_post请求例子的更多相关文章
- 一个vue请求接口渲染页面的例子
new Vue({ el:'#bodylist', data: { list: [ { "type_id": "1", "type_name" ...
- 一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。
一个封装的使用Apache HttpClient进行Http请求(GET.POST.PUT等)的类. import com.qunar.payment.gateway.front.channel.mp ...
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- 一个UWSGI的例子
摘要:uwsgi执行顺序:启动master进程,执行python脚本的公共代码(import同一层).然后生成worker进程,uwsgi.post_fork_hook=init_functions, ...
- HTTP_POST请求的数据格式
HTTP_POST请求的数据格式 在HTTP的请求头中,可以使用Content-type来指定不同格式的请求信息. Content-type的类型 常见的媒体格式类型: text/html : ...
- HttpClientUtil [使用apache httpclient模拟http请求]
基于httpclient-4.5.2 模拟http请求 以get/post方式发送json请求,并获取服务器返回的json -------------------------------------- ...
- 【JAVA】通过HttpClient发送HTTP请求的方法
HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 一步步教你为网站开发Android客户端---HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新ListView
本文面向Android初级开发者,有一定的Java和Android知识即可. 文章覆盖知识点:HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新List ...
随机推荐
- Python day1_Base1笔记
1.helloworld print('helloword') 2.输入输出 a=input('Please input a value') print(a) 3.标识符 1.由字母数字下划线构成 2 ...
- VC.遍历文件夹中的文件
1.VC下遍历文件夹中的所有文件的几种方法 - 年少要轻狂 - CSDN博客.html(https://blog.csdn.net/wllmsdn/article/details/27220999) ...
- Qt532.QString::split()
1.效果 和 JS里面 貌似是一样的 1.1.QString 编码转换(https://www.cnblogs.com/CodeSkill/p/5082447.html) 2.代码: void Mai ...
- [原][粒子特效][spark]发射器emitter
深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html group添加emitter的方式: eimtter: 上图是spark源码 ...
- python模块(3)
1.xml:实现不同语言或程序之间进行数据交换的协议 <data> <country name="Liechtenstein"> <rank upda ...
- Java创建多线程和线程安全集合Vector
public class Test { public static Vector<String> data = new Vector<String>(); public sta ...
- springBoot springSecurty: x-frame-options deny禁止iframe调用
springBoot springSecurty: x-frame-options deny禁止iframe调用 https://blog.csdn.net/whiteforever/article ...
- [转载]mapreduce合并小文件成sequencefile
mapreduce合并小文件成sequencefile http://blog.csdn.net/xiao_jun_0820/article/details/42747537
- kibana的 timelion工具
时序控件(Timelion)是一款时间序列数据可视化工具,它可以将多种独立的数据源合并呈现到一张视图上. .es函数 index 指明索引 .es(index=nginx-access-log- ...
- S-Nim HDU - 1536
#include<iostream> #include<cstdio> #include<cstring> #include<vector> using ...