http协议中post方法发出请求
package com.chinaums.szm.test.base.igoodful; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test; import java.io.IOException;
import java.util.*; public class TestPost { public String doPost(String url, Map<String,String> map, String charset){
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = HttpClients.createDefault();
httpPost =new HttpPost(url);
List<NameValuePair> list = new ArrayList<>();
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,String> entry =(Map.Entry<String,String>) iterator.next();
list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
if (list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
httpPost.setEntity(entity);
}
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse != null){
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null){
result = EntityUtils.toString(httpEntity,charset);
}
}
}catch (Exception ex){
ex.printStackTrace();
}
return result;
}
@Test
public void test() throws IOException {
String url = "http://admin.tingwen.me/index.php/api/interfaceXykj/touList";
Map<String,String> map = new HashMap<>();
map.put("page","100");
String result = this.doPost(url,map,"UTF-8");
//将输出更加优美的方式展示出。
ObjectMapper mapper = new ObjectMapper();
Object object = mapper.readValue(result,Object.class);
System.out.println( mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object));
}
}
http协议中post方法发出请求的更多相关文章
- http协议中客户端8种请求方法
http请求中的8种请求方法 1.opions 返回服务器针对特定资源所支持的HTML请求方法 或web服务器发送*测试服务器功能(允许客户端查看服务器性能) 2.Get 向特定资源发出请 ...
- HTTP协议中的1xx,2xx,3xx,4xx,5xx状态码分别表示什么,列举常见错误码及含义
转自:http://m.blog.csdn.net/blog/u013857407/21741847 HTTP协议状态码,是指在HTTP协议运作中由客户端发出请求连接,服务端建立连接,客户端发出HTT ...
- Block作为property属性实现页面之间传值(代替Delegate代理与协议结合的方法)
需求:在ViewController中,点击Button,push到下一个页面NextViewController,在NextViewController的输入框TextField中输入一串字符,返回 ...
- Objective-C 中的方法回掉
Objective-C 中的方法回掉 Objective-C 中的方法调用 OC 类似C+,避免不了的内部的各种通信手段,函数调用,变量传递等,避免不了各种回掉等. 一.class直接调用 或者 o ...
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误
(来源:http://www.tuicool.com/articles/Ermmmyn) HTTP请求方法: 常用方法: Get\Post\Head (1)Get方法. 取回请求URL标志的任何信息, ...
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误 #Reprinted#
请求方法是请求一定的Web页面的程序或用于特定的URL. 可选用下列几种: GET: 请求指定的页面信息,并返回实体主体. HEAD: 只请求页面的首部. POST: 请求服务器接受所指定的文档作为对 ...
- http协议中:GET/POST/PUT/DELETE/TRACE/OPTIONS/HEAD方法
###1 HTTP/1.1协议中共定义了八种方法(有时也叫"动作")来表明Request-URI指定的资源的不同操作方式: OPTIONS 返回服务器针对特定资源所支持的HTTP请 ...
- 简单了解HTTP协议的基本知识,请求流程、请求方法等
HTTP 是Hyper Text Transfer Protocol(超文本传输协议)的缩写 1.超文本传输协议是一种详细规定了浏览器和万维网服务器之间互相通信的规则. 2.HTTP协议(HyperT ...
- http中post 和 get 请求方法区别
前言 做Web开发就一定会涉及到浏览器和服务器的交互,所以了解浏览器和服务器交互的方式就尤为重要.从接触B/S开始就已经接触到了get和post,但是对它们的了解确实不深入.在后来不断的做项目过程中, ...
随机推荐
- ThinkPHP项目笔记之数据库配置篇
对于配置文件,有几点说明 common:公共配置,也就是前台,后台,都可以调用的文件,具有普遍性 前台/后台:就是针对前后台的配置文件,具有针对性. 如:(公共文件基本配置) <?php ret ...
- C# 重命名文件
在vb下,有一个方法可以对文件进行重命名,所以,我们只要引用到这种方法进行应用即可. 1.添加引用Microsoft.VisualBasic 2.添加命名空间using Microsoft.Visua ...
- UIStoryboard小提示
- (id)instantiateInitialViewController - (id)instantiateViewControllerWithIdentifier:(NSString *)ide ...
- 编程之美 set 14 小飞的电梯调度算法
题目 电梯每次上升只停一次, 求问电梯停在哪一楼能够保证乘坐电梯的所有乘客爬楼层的层数之和最小 思路 假设电梯的层数是 m, 乘客人数是 n 1. 枚举, 时间复杂度是 o(mn) 2. 滚动解法. ...
- ionic触摸事件
官方文档:http://ionicframework.com/docs/api/directive/onHold/ on-hold 长按事件on-tap 点击事件 on-double-tap 双击事 ...
- 二、Android Studio使用——导入jar包,运行、debug都不是问题
[新建AndroidStudio工程,lib导入jar包] 我们的项目代码都在app里面,可以看作是一个Model. src 下面除了我们的代码之外,还有单元测试. 把JAR复制到libs文件 ...
- [Chrome] 如何下载老版本的 Chrome
Google 官方只提供了最新版的 Chrome, 在旧版本的系统(如:Ubuntu 12.04 LTS)安装不上 这里提供了 Chrome 的历史版本下载 备注:Ubuntu 12.04 LTS 可 ...
- JAVA基础之multipart,urlencoded以及JSON
一.(enctype) 表单的默认编码方式 ajpplication/x-www-form-urlencoded 上传文件的编码方式 multipart/form-data 互联网应用常用编码 ...
- Exchange Database Status(Copy Status ,Content Index State,QueueLength,Move Status...)
Copy Status Description Mounted The active copy is online and accepting client connections. Only the ...
- hook Extending the Framework Core
w Task Scheduling - Laravel - The PHP Framework For Web Artisanshttps://laravel.com/docs/5.4/schedul ...