JAVA发送POST请求时--如何携带(请求参数,请求头参数)呢?

欣喜 Java经验 发布时间:2024-01-30 15:25:29 阅读数:13329 1
下文笔者讲述java发送post请求时-携带参数的方法及示例分享,如下所示
1.引入httpClient依赖
2.创建参数集合
3.使用 post.setEntity 将参数集合放入
4.使用 post.addHeader添加头部参数
例:POST请求的示例
 
//1.引入相应的依赖

  <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="java265";

        //创建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","2024-01-30 15:23:11"));
            //把参数放入请求对象,,post发送的参数list,指定格式
            post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
            //添加请求头参数
            post.addHeader("timestamp","1591374606331");
            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 "";

        }
    }
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202401/17065995687855.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者