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();
}
}
HTTP请求(常用于请求第三方接口)
最新推荐文章于 2023-02-14 15:13:34 发布