HTTP请求(常用于请求第三方接口)

package com.prophesy.util;

import lombok.extern.slf4j.Slf4j;

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

/**
 * @ClassName: HttpUtils
 * @Description:
 * @Author : leo
 * @Date: 2021/12/6 17:34
 */
@Slf4j
public class HttpUtils {

    public static String httpPostData(String urlStr, String postData, Map<String,String> headMap) throws Exception {
        log.info("postData  ===="+postData);
        HttpURLConnection conn = null;
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        InputStream is = null;
        try {
            // 发送POST请求
            URL url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("Connection", "Keep-Alive");
            if(headMap != null){
                for (Map.Entry<String, String> head : headMap.entrySet()) {
                    conn.setRequestProperty(head.getKey(), head.getValue());
                }
            }
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Length", "" + postData.length());
            out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
            out.write(postData);
            out.flush();
            // 获取响应状态
            if (conn.getResponseCode() >= 400) {
                is = conn.getErrorStream();
            } else {
                is = conn.getInputStream();
            }
            // 获取响应内容体
            String line;
            in = new BufferedReader(new InputStreamReader(is, "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
        } catch (IOException e) {
            e.printStackTrace(System.out);
            throw new Exception("连接失败");
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        System.out.println("result  ===="+result);
        return result;
    }

    public static String httpGetData(String urlStr,Map<String,String> headerMap) {
        String DEFAULT_ENCODING = "UTF-8";
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL(urlStr.trim());
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            if(headerMap != null){
                for (Map.Entry<String, String> header : headerMap.entrySet()) {
                    conn.setRequestProperty(header.getKey(),header.getValue());
                }
            }
            // conn.setConnectTimeout(CONNECT_TIMEOUT);
            // conn.setReadTimeout(CONNECT_TIMEOUT);
            conn.connect();
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    public static String httpGetData(String urlStr) {
        String DEFAULT_ENCODING = "UTF-8";
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL(urlStr.trim());
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.connect();
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值