原文: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. 出现 sudo: unable to resolve host XXX 信息解决办法

    Ubuntu环境,  每次执行sudo 就出现这个警告讯息:sudo: unable to resolve host XXX虽然sudo 还是可以正常执行,是机器在反解上的问题, 所以就直接从/etc ...

  2. Flask常用路由参数

    Flask中的路由参数: @app.route(‘/’, endpoint=’xx’ , methods=[‘GET’,...]) >endpoint后的名字,用来反向生成url的.后面的名字随 ...

  3. 深入js系列-类型(数字)

    开头 js数字没有明确区分浮点数和整数类型,统一用number类型表示. number 基于IEEE 754标准实现 js采用的是双精度(64位二进制) 我们看一个基于IEEE 754标准实现都有会有 ...

  4. 第10组 Beta冲刺(2/4)

    队名:凹凸曼 组长博客 作业博客 组员实践情况 童景霖 过去两天完成了哪些任务 文字/口头描述 编写商品主界面 展示GitHub当日代码/文档签入记录 暂无代码 接下来的计划 编写购买功能 还剩下哪些 ...

  5. listings技巧

    1. \lstdefinestyle 参考 https://blog.csdn.net/ProgramChangesWorld/article/details/52142313 我们在使用listin ...

  6. HttpClient代理IP及设置连接读取超时

    1.不废话,上代码: public static void main(String[] args) throws Exception { CloseableHttpClient httpClient ...

  7. [HeadFrist-HTMLCSS学习笔记]第三章构建模块:Web页面建设

    [HeadFrist-HTMLCSS学习笔记]第三章构建模块:Web页面建设 敲黑板!! <q>元素添加短引用,<blockquote>添加长引用 在段落里添加引用就使用< ...

  8. c语言之连接符

    c语言之连接符 1.连接符 连接符的概念是结合define预编译指令的使用技巧,用户可以向define中传入字符串来调用不同功能的函数. 2.代码例子 #include <stdio.h> ...

  9. php 跳出循环 break

    break语句可以带一个参数n,表示跳出循环的层数,如果要跳出多重循环的话,可以用n来表示跳出的层数,如果不带参数默认是跳出本重循环.

  10. C语言结构体的“继承”

    这里说的继承有点像C++里的父类和子类,实际上是结构体类型的强制转换,最近看Linux内核源码时经常接触到这种方法,在这里就当作是一个简单的学习吧. 下面给出一个Demo,很简单,分别定义了一个fat ...