原文: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. selenium webdriver使用的一些小技巧(持续更新中)

    1.开始结束时间只支持控件选择,不支持填写,怎么办? 如下图: 解决方案: 用javaScipt把开始结束时间的reaonly属性去除,然后再输入,举例如下 /**     * 输入开始日期     ...

  2. LeetCode 1213. Intersection of Three Sorted Arrays

    原题链接在这里:https://leetcode.com/problems/intersection-of-three-sorted-arrays/ 题目: Given three integer a ...

  3. Codeforces Round #606 (Div. 2) D - Let's Play the Words?(贪心+map)

  4. json 字符串 反序列化

    private void button17_Click(object sender, EventArgs e) { string s = "{\"returnCode\" ...

  5. 洛谷 P3373 【模板】线段树 2 题解

    P3373 [模板]线段树 2 题目描述 如题,已知一个数列,你需要进行下面三种操作: 1.将某区间每一个数乘上x 2.将某区间每一个数加上x 3.求出某区间每一个数的和 输入格式 第一行包含三个整数 ...

  6. SQL 变形

    需求:共有协议X份,已签XX份,待签X份 sql: select count(1) 总记录数, sum(case when XY_STATUS='1' then 1 else 0 end)待签, su ...

  7. .NET三层架构开发初步

    转自:https://www.cnblogs.com/weilengdeyu/archive/2013/01/04/2844847.html 今天咱们来谈下三层架构.说到三层架构,搞过点程序的可能都知 ...

  8. 一个sh脚本 同时运行 多个sh脚本

    问题: 原来的启动方式 cd /opt/OpenIMSCore ./pcscf.sh ./icscf.sh ./scscf.sh cd /opt/OpenIMSCore/FHoSS/deploy ./ ...

  9. Sourcetree 集成 Azure DevOps Server(Git)

    概述- SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端管理工具,同时也是Mercurial和Subversion版本控制系统工具.支持创建.克隆.提交 ...

  10. linux -root和文件属性、权限等

    1.默认简易安装的ubuntu没有root用户,第一次创建root密码可以使用 sudo passwd 命令设置新的root密码 zzh@ubuntu:~$ sudo passwdEnter new ...