JAVA攜帶參數(帶有請求參數,請求頭參數)直接發送POST請求


 

 

 

  <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.3</version>
        </dependency>

 

 

 

package com.test;

import org.apache.http.HttpEntity;
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;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        sendPost();
    }

    public static void sendPost(){
        String value="12222";

        //創建post請求對象
        HttpPost post = new HttpPost("http://localhost:8080/test");
        try {
            //創建參數集合
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
            //添加參數
            list.add(new BasicNameValuePair("key", value));
            list.add(new BasicNameValuePair("releaseDate","2020-07-14 09:55:20"));
            //把參數放入請求對象,,post發送的參數list,指定格式
            post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
            //添加請求頭參數
            post.addHeader("timestamp","1594695607545");
            CloseableHttpClient client = HttpClients.createDefault();
            //啟動執行請求,並獲得返回值
            CloseableHttpResponse response = client.execute(post);
            //得到返回的entity對象
            HttpEntity entity = response.getEntity();
            //把實體對象轉換為string
            String result = EntityUtils.toString(entity, "UTF-8");
            //返回內容
            System.out.println(result);
        } catch (Exception e1) {
            e1.printStackTrace();

        }
    }
}

 

下面是把方法進行了封裝

/**
     *
     * 發送請求
     * @param url 發送的url
     * @param headerMap  請求頭參數集合 key參數名 value為參數值
     * @param bodyMap   請求參數集合 key參數名 value為參數值
     */
    public static String sendPost(String url, Map<String,String> headerMap,Map<String,String> bodyMap){

        //創建post請求對象
        HttpPost post = new HttpPost(url);
        try {
            //創建參數集合
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
            //添加參數
            if (bodyMap!=null){
                for (String str:bodyMap.keySet()
                ) {
                    list.add(new BasicNameValuePair(str, bodyMap.get(str)));
                }
            }
            //把參數放入請求對象,,post發送的參數list,指定格式
            post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));


            if (headerMap!=null){
                for (String str:headerMap.keySet()
                     ) {
                    post.addHeader(str,headerMap.get(str));
                }
            }

            CloseableHttpClient client = HttpClients.createDefault();
            //啟動執行請求,並獲得返回值
            CloseableHttpResponse response = client.execute(post);
            //得到返回的entity對象
            HttpEntity entity = response.getEntity();
            //把實體對象轉換為string
            String result = EntityUtils.toString(entity, "UTF-8");
            //返回內容
            return result;
        } catch (Exception e1) {
            e1.printStackTrace();
            return "";

        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM