首页 > 技术文章 > httpPost请求用java代码实现的方法

gyadmin 2017-12-27 14:16 原文

原文: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
 */

 

推荐阅读