httpPost请求用java代码实现的方法
原文:https://www.cnblogs.com/johnson-yuan/p/6713384.html
package com.day3.sample;
//首先下面我我们需要导入的jar包和文件
import java.io.IOException; import java.util.ArrayList; import java.util.List;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientDemo2 {
//构造POST请求
public void test_post(){
//新建一个客户对象
CloseableHttpClient client = HttpClients.createDefault();
//新建一个HttpPost请求的对象 --并将uri传给这个对象
HttpPost post = new HttpPost("http://localhost:8080/test1312/Calc");
//使用NameValuePair 键值对 对参数进行打包
List<NameValuePair> list = new ArrayList<NameValuePair>
list.add(new BasicNameValuePair("a","1"));
list.add(new BasicNameValuePair("b","2"));
//4.对打包好的参数,使用UrlEncodedFormEntity工具类生成实体的类型数据
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,Consts.UTF_8);
//5.将含有请求参数的实体对象放到post请求中
post.setEntity(entity);
//6.新建一个响应对象来接收客户端执行post的结果
CloseableHttpResponse response = client.execute(post);
//7.从响应对象中提取需要的结果-->实际结果,与预期结果进行对比
if(response.getStatusLine().getStatusCode() == 200){
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
}
//编写过程中如果有异常请选择shrow这个异常
//然后对这个类中的方法进行调用就可以实现了对接口的测试了
package com.day3.sample;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
public class HttpClientDemo2Test {
public static void main(String[] args) throws ClientProtocolException, IOException {
HttpClientDemo2 demo2 = new HttpClientDemo2();
demo2.test_post();
}
}
/*
* Project: payment.wechat.api
*
* File Created at 2017年12月27日
*
* Copyright 2016 CMCC Corporation Limited.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* ZYHY Company. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license.
*/
package com.cmcc.hybj.payment.wechatapi.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* @Type HttpClientDemo2.java
* @Desc //构造post请求
* @author gy
* @date 2017年12月27日 下午2:20:52
* @version
*/
public class HttpClientDemo2 {
//构造post请求
public void test_post() throws ClientProtocolException, IOException {
//新建一个客户对象
CloseableHttpClient client = HttpClients.createDefault();
//新建一个HttpPost请求的对象 --并将uri传给这个对象
HttpPost post = new HttpPost("http://localhost:8080/test1312/Calc");
//使用NameValuePair 键值对 对参数进行打包
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("a", "1"));
list.add(new BasicNameValuePair("b", "2"));
//4.对打包好的参数,使用UrlEncodedFormEntity工具类生成实体的类型数据
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, Consts.UTF_8);
//5.将含有请求参数的实体对象放到post请求中
post.setEntity(entity);
//6.新建一个响应对象来接收客户端执行post的结果
CloseableHttpResponse response = client.execute(post);
//7.从响应对象中提取需要的结果-->实际结果,与预期结果进行对比
if (response.getStatusLine().getStatusCode() == 200) {
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
public static void main(String[] args) throws ClientProtocolException, IOException {
HttpClientDemo2 demo2 = new HttpClientDemo2();
demo2.test_post();
}
}
/**
* Revision history
* -------------------------------------------------------------------------
*
* Date Author Note
* -------------------------------------------------------------------------
* 2017年12月27日 Administrator creat
*/
httpPost请求用java代码实现的方法的更多相关文章
- httpget请求测试用Java代码的实现方法
原文:http://www.cnblogs.com/johnson-yuan/p/6637906.html 1.首先要在eclipse中导入HttpClient的jar包. 2.新建类并写入一下代码: ...
- JAVA代码MD5加密方法
PwdEncoder.java 接口类 package com.common.security.encoder; /** * 密码加密接口 */ public interface PwdEncoder ...
- java代码-------继承的方法----重写还是重载
总结:是自己不听讲吧,不懂啊 感觉父类的方法,子类可以重载,只要参数个数不同,重载与返回值没有关系 重写绝对是可以的.但答案是只能重写啊 package com.s.x; public class T ...
- java代码---------常用的方法indexOf()和substring()方法的小结、主要是下标都是从0开始,很重要,错了就那个差远了啊
package com.s.x; //indexOf()方法从字符起始处的第一个位子开始的位置 //substring public class Wang { public static void m ...
- java代码啊==indexOf()方法返回字符第一次出现的位置
package com.s.x; public class Wang { public static void main(String[] args) { if ("woaini" ...
- Android开发之 当前日期String类型转date类型 java代码中实现方法
/** * 获取当前时间 * * @return */ public Date getDate(String str) { try { java.text.SimpleDateFormat forma ...
- java代码获知该方法被哪个类、哪个方法、在哪一行调用
public class TestMain { public static void main(String[] args){ hello(); } public static void hello( ...
- 【转】使用JavaParser获得Java代码中的类名、方法形参列表中的参数名以及统计总的文件个数与不能解析的文件个数
遍历目录查找Java文件: public static void ergodicDir(File dir, HashSet<String> argNameSet, HashSet<S ...
- 使用Java代码来创建view
使用Java代码来创建view 一.简介 需要了解的知识 二.方法 1)java代码创建view方法 * 1.先建view对象 View view= View.inflate(this, R.layo ...
随机推荐
- Codeforces Round #135 (Div. 2) D - Choosing Capital for Treeland(两种树形DP)
- A Deep Dive into PL/v8
Back in August, Compose.io announced the addition of JavaScript as an internal language for all new ...
- django -- ORM查询
前戏 在我们之前操作ORM中,你也许是启动Django项目,通过地址访问固定的函数,或者在pycharm里的python console里执行,第一种比较麻烦,而且每次都要启动项目,写路由,第二种虽然 ...
- STL——list用法总结
头文件 #include<list> 声明一个int型的list:list<int> a: 1.list的构造函数 list<int>a{1,2,3} list&l ...
- Socket网络编程——C++实现
本代码可直接使用 根据TCP/IP三次握手,实验时可使用两台电脑,或者打开两个终端模拟通信. 服务器端: #include <iostream> #include <windows. ...
- hdu 6620 Just an Old Puzzle(N数码问题)
http://acm.hdu.edu.cn/showproblem.php?pid=6620 N数码问题: n*n矩阵,里面填着1—n*n-1,还有1个空格, 通过上下左右移动空格的位置, 使矩阵里的 ...
- GitHub for mobile 来了,码农苦逼了!
北京时间 2019 年 11 月 14 日 GitHub Universe 2019 大会上,GitHub 正式发布了 GitHub for mobile,即 GitHub 的移动版本,支持 iOS ...
- shell(一) shell变量
基本介绍 变量命名规范 变量名要求由字母.数字.下划线组成,尽量字母开头,有明确含义 注意:变量赋值时,等号前后不能有空格,变量名称不能和字体变量冲突 自定义变量 当前shell有效 1.定义变量 v ...
- 将物理机转换成vmware虚机
随着虚拟化的快速发展,公司主要是以公有云+私有云结合的混合云部署,据我不成熟的了解,目前很少有公司会将一台单独的物理机作为服务器,在公司内部大家逐渐接受了私有云的部署方案,这样做不但可以节省硬件资源, ...
- Kubernetes之Taints与Tolerations 污点和容忍
NodeAffinity节点亲和性,是Pod上定义的一种属性,使Pod能够按我们的要求调度到某个Node上,而Taints则恰恰相反,它可以让Node拒绝运行Pod,甚至驱逐Pod. Taints(污 ...