栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


@Slf4j
public class HttpUtils {

    public static String httpPostData(String urlStr, String postData, Map 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 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 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 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("rn");
            }
        } 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("rn");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976155.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号