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,但是对它们的了解确实不深入.在后来不断的做项目过程中, ...
随机推荐
- MathType中输入不了汉字如何处理
MathType作为一款常见的数学公式编辑器在编辑数学公式时,不仅可以输入英文字符,对中文也有很好的兼容性.但是有些用户在使用MathType编辑公式时,发现一些汉字是输入不进去的,这个时候我们就需要 ...
- Nginx配置里的fastcgi_index和index
在配置nginx时有时会遇到, 所以记录一下 location ^~ /wechat/ { index index.php; fastcgi_pass 127.0.0.1:9000; fastcgi_ ...
- Struts2_day01--访问action的方法
访问action的方法(重点) 1 有三种方式实现 第一种 使用action标签的method属性,在这个属性里面写执行的action的方法名称 第二种 使用通配符方式实现 第三种 动态访问实现(不用 ...
- linux 常用命令总结(tsg)
1.解压tar.gz 第一步:gunzip filename.tar.gz --->会解压成.tar文件 第二步:tar xvf FileName.tar ---->会解压到当前文件夹2. ...
- Qt slot中获取sender
调用sender();函数 例如获取一个QRadioButton QRadioButton *rb = qobject_cast<QRadioButton *>(sender());
- 『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于AD)
一.进管理中心,创建一个应用程序,配置如下: 二.填端口号,和选择form身份认证,以及填写成员和角色,其他都默认就可以了 三.使用SharePoint 2010 Management Shell在里 ...
- java如何发起一次http的post请求?
@RequestMapping(value = "loginInSSO", method = RequestMethod.GET) public String loginInSSO ...
- mysql用sql创建表完整实例
create table user_login_latest( id int(11) unsigned NOT NULL AUTO_INCREMENT, user_id int(11) not nul ...
- html5文本超出部分用省略号表示
<p style="overflow:hidden; text-overflow:ellipsis;width:170px; white-space:nowrap; "> ...
- java -jar后台启动
nohup java -jar XX.jar >logs.log &