原文: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代码实现的方法的更多相关文章

  1. httpget请求测试用Java代码的实现方法

    原文:http://www.cnblogs.com/johnson-yuan/p/6637906.html 1.首先要在eclipse中导入HttpClient的jar包. 2.新建类并写入一下代码: ...

  2. JAVA代码MD5加密方法

    PwdEncoder.java 接口类 package com.common.security.encoder; /** * 密码加密接口 */ public interface PwdEncoder ...

  3. java代码-------继承的方法----重写还是重载

    总结:是自己不听讲吧,不懂啊 感觉父类的方法,子类可以重载,只要参数个数不同,重载与返回值没有关系 重写绝对是可以的.但答案是只能重写啊 package com.s.x; public class T ...

  4. java代码---------常用的方法indexOf()和substring()方法的小结、主要是下标都是从0开始,很重要,错了就那个差远了啊

    package com.s.x; //indexOf()方法从字符起始处的第一个位子开始的位置 //substring public class Wang { public static void m ...

  5. java代码啊==indexOf()方法返回字符第一次出现的位置

    package com.s.x; public class Wang { public static void main(String[] args) { if ("woaini" ...

  6. Android开发之 当前日期String类型转date类型 java代码中实现方法

    /** * 获取当前时间 * * @return */ public Date getDate(String str) { try { java.text.SimpleDateFormat forma ...

  7. java代码获知该方法被哪个类、哪个方法、在哪一行调用

    public class TestMain { public static void main(String[] args){ hello(); } public static void hello( ...

  8. 【转】使用JavaParser获得Java代码中的类名、方法形参列表中的参数名以及统计总的文件个数与不能解析的文件个数

    遍历目录查找Java文件: public static void ergodicDir(File dir, HashSet<String> argNameSet, HashSet<S ...

  9. 使用Java代码来创建view

    使用Java代码来创建view 一.简介 需要了解的知识 二.方法 1)java代码创建view方法 * 1.先建view对象 View view= View.inflate(this, R.layo ...

随机推荐

  1. [Javascript] Creating an Iterator from an Array

    Every Array has a function which you can use to create an iterator. This function can only be access ...

  2. three.js 添加环境光

    var ambient = new THREE.AmbientLight(0xffffff);//环境光 scene.add(ambient);

  3. 请解释一下css3的flexbox(弹性盒布局模型),以及适用场景

    弹性盒模型是c3规范的新的布局方式,该布局模型的目的是提供一种更加高效的方式来对容器的条目进行布局.对齐和分配空间.在传统的布局中,block布局是把块级元素在垂直方向从上向下一次排列的,而inlin ...

  4. (转)Qt中文手册 之 QApplication

    QApplication管理GUI程序的控制流和主要设置. QApplication包含由窗口系统和其他来源处理过和发送过的主事件循环.它也处理应用程序的初始化和收尾工作,并提供对话管理.QAppli ...

  5. Docker环境下的前后端分离项目部署与运维(六)搭建MySQL集群

    单节点数据库的弊病 大型互联网程序用户群体庞大,所以架构必须要特殊设计 单节点的数据库无法满足性能上的要求 单节点的数据库没有冗余设计,无法满足高可用 单节点MySQL的性能瓶领颈 2016年春节微信 ...

  6. 贪心 --- Y2K Accounting Bug

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9691   Accepted: 483 ...

  7. 中级java面试经历

    2018年已经远去,2019年悄然而至.跳槽不仅是为了涨薪,更是为了锻炼自己,提高自己的能力.树挪死,人挪活.在一个公司呆的时间越长,就越老油条,从而失去不断前进的动力.现在下面就主要讲述我这一个月面 ...

  8. 解决source insight 4.0 不识别.cc文件的问题

    Options -> File Type Options, File Filter 中加入,*.cc 参考了C++ Primer Plus第五版中文版 P8 C++实现 源代码的扩展名 UNIX ...

  9. pychram 激活码

    转自博客:https://blog.csdn.net/may_ths/article/details/84032217 激活码到期时间: 2020.06 K6IXATEF43-eyJsaWNlbnNl ...

  10. 如何选择CPU

    一.品牌: 选择哪家公司的处理器,AMD公司和inter公司的处理器相比较,AMD在三维制作.游戏应用.和视频处理方面突出,inter的处理器在商业应用.多媒体应用.平面设计方面有优势,性能方面,同档 ...