com.gitee.fufu669.utils.CacheHttpUtil Maven / Gradle / Ivy
package com.gitee.fufu669.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;
public class CacheHttpUtil {
public static final Logger logger = LoggerFactory.getLogger(CacheHttpUtil.class);
// post请求
public static final String HTTP_POST = "POST";
// get请求
public static final String HTTP_GET = "GET";
// utf-8字符编码
public static final String CHARSET_UTF_8 = "utf-8";
// HTTP内容类型。如果未指定ContentType,默认为TEXT/HTML
public static final String CONTENT_TYPE_TEXT_HTML = "text/xml";
// HTTP内容类型。相当于form表单的形式,提交暑假
public static final String CONTENT_TYPE_FORM_URL = "application/x-www-form-urlencoded";
// 请求超时时间
public static final int SEND_REQUEST_TIME_OUT = 50000;
// 将读超时时间
public static final int READ_TIME_OUT = 50000;
public static String getAutoCharSet(String urlStr) {
return get(urlStr);
}
public static String get(String urlStr) {
return get(urlStr, "utf-8");
}
/**
* 将map集合的键值对转化成:key1=value1&key2=value2 的形式
*
* @param parameterMap 需要转化的键值对集合
* @return 字符串
*/
public static String convertStringParamter(Map parameterMap) {
StringBuffer parameterBuffer = new StringBuffer();
if (parameterMap != null) {
Iterator iterator = parameterMap.keySet().iterator();
String key = null;
String value = null;
while (iterator.hasNext()) {
key = (String) iterator.next();
if (parameterMap.get(key) != null) {
value = (String) parameterMap.get(key);
} else {
value = "";
}
parameterBuffer.append(key).append("=").append(value);
if (iterator.hasNext()) {
parameterBuffer.append("&");
}
}
}
return parameterBuffer.toString();
}
// public static void main(String[] args) throws MalformedURLException {
//
// System.out.println(getOrPost(HTTP_GET, "http://127.0.0.1:8080/test/TestHttpRequestServlet",
// "username=123&password=我是谁"));
//
// }
public static String get(String urlStr, String charset) {
// 是否有http正文提交
boolean isDoInput = false;
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
try {
// 统一资源
URL url = new URL(urlStr);
// 连接类的父类,抽象类
URLConnection urlConnection = url.openConnection();
// http的连接类
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
// 设置是否从httpUrlConnection读入,默认情况下是true;
httpURLConnection.setDoInput(true);
// 设置一个指定的超时值(以毫秒为单位)
httpURLConnection.setConnectTimeout(SEND_REQUEST_TIME_OUT);
// 将读超时设置为指定的超时,以毫秒为单位。
httpURLConnection.setReadTimeout(READ_TIME_OUT);
// Post 请求不能使用缓存
httpURLConnection.setUseCaches(false);
// 设置字符编码
httpURLConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
httpURLConnection.setRequestProperty("Cache-Control", "max-age=0");
httpURLConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
// 设定请求的方法,默认是GET
httpURLConnection.setRequestMethod("GET");
// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
// 如果在已打开连接(此时 connected 字段的值为 true)的情况下调用 connect 方法,则忽略该调用。
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() >= 300) {
if (httpURLConnection.getResponseCode() == 302) {
String location = httpURLConnection.getHeaderField("location");
System.out.println("访问url:"+urlStr+":重定向:"+location);
return get(location, charset);
}
throw new Exception(
"HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream();
if (StringUtils.isEmpty(charset)) {
charset = "utf-8";
}
inputStreamReader = new InputStreamReader(inputStream, charset);
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
resultBuffer.append("\n");
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {// 关闭流
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (inputStreamReader != null) {
inputStreamReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return resultBuffer.toString();
}
public static String get302Url(String urlStr, String charset) {
// 是否有http正文提交
boolean isDoInput = false;
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
try {
// 统一资源
URL url = new URL(urlStr);
// 连接类的父类,抽象类
URLConnection urlConnection = url.openConnection();
// http的连接类
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
// 设置是否从httpUrlConnection读入,默认情况下是true;
httpURLConnection.setDoInput(true);
// 设置一个指定的超时值(以毫秒为单位)
httpURLConnection.setConnectTimeout(SEND_REQUEST_TIME_OUT);
// 将读超时设置为指定的超时,以毫秒为单位。
httpURLConnection.setReadTimeout(READ_TIME_OUT);
// Post 请求不能使用缓存
httpURLConnection.setUseCaches(false);
// 设置字符编码
httpURLConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
httpURLConnection.setRequestProperty("Cache-Control", "max-age=0");
httpURLConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
// 设定请求的方法,默认是GET
httpURLConnection.setRequestMethod("GET");
// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
// 如果在已打开连接(此时 connected 字段的值为 true)的情况下调用 connect 方法,则忽略该调用。
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() >= 300) {
if (httpURLConnection.getResponseCode() == 302) {
String location = httpURLConnection.getHeaderField("location");
return location;
}
throw new Exception(
"HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
return "";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {// 关闭流
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (inputStreamReader != null) {
inputStreamReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return resultBuffer.toString();
}
}