import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: 
 * @description:
 * @author: daijm
 * @create: 2022-07-11 15:10
 */
public class test {
    public  static void main(String[] args) throws Exception {
        //请求参数
        Map<String, String> headers=new HashMap<>();
        headers.put("startDate","2022-02-02");
        headers.put("endDate","2022-02-02");

        //请求地址
        String requestURL="http://127.0.0.1:8070/in";
        openUrl(requestURL,headers);
    }

    public static String openUrl(String url, Map<String,String> parameter) throws Exception {
        URL urls = new URL(url);
        HttpURLConnection connection = null;
        OutputStream outputStream = null;
        String rs = null;
        try {
            connection = (HttpURLConnection) urls.openConnection();
            connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----footfoodapplicationrequestnetwork");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
            connection.setRequestProperty("Accept", "*/*");
            connection.setRequestProperty("Range", "bytes="+"");
            connection.setConnectTimeout(8000);
            connection.setReadTimeout(20000);
            connection.setRequestMethod("POST");

            StringBuffer buffer = new StringBuffer();
            if(parameter != null) {
                //设置请求参数
                for(String key :parameter.keySet()) {
                    buffer.append("------footfoodapplicationrequestnetwork\r\n");
                    buffer.append("Content-Disposition: form-data; name=\"");
                    buffer.append(key);
                    buffer.append("\"\r\n\r\n");
                    buffer.append(parameter.get(key));
                    buffer.append("\r\n");
                }
                buffer.append("------footfoodapplicationrequestnetwork--\r\n");
            }

            outputStream = connection.getOutputStream();
            outputStream.write(buffer.toString().getBytes());
            try {
                connection.connect();
                if(connection.getResponseCode() == 200) {
                    System.out.println("------------------------------");
                }
            }
            catch (Exception e) {
                rs = null;
            }

            return rs;
        }
        finally {
            try {
                outputStream.close();
            }
            catch (Exception e) {
            }
            outputStream = null;

            if(connection != null) {
                connection.disconnect();
            }
            connection = null;
        }
    }
}

版权声明:本文为jinming1109原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/jinming1109/article/details/125725719