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