QQBot/src/main/java/com/yutou/qqbot/utlis/HttpTools.java

347 lines
14 KiB
Java
Raw Normal View History

2021-12-06 11:19:00 +08:00
package com.yutou.qqbot.utlis;
import com.alibaba.fastjson2.JSONObject;
2021-12-06 11:19:00 +08:00
import com.yutou.qqbot.interfaces.DownloadInterface;
2022-02-08 12:27:07 +08:00
import org.jetbrains.annotations.NotNull;
2021-12-06 11:19:00 +08:00
2022-01-02 00:14:27 +08:00
import javax.net.ssl.HttpsURLConnection;
2021-12-06 11:19:00 +08:00
import java.io.*;
import java.net.*;
2022-04-08 02:26:29 +08:00
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
2021-12-06 11:19:00 +08:00
import java.util.Map;
import java.util.Set;
public class HttpTools {
public static final String serverKey = "zIrsh9TUZP2lfRW753PannG49E7VJvor";
private static final int HttpRequestIndex = 3;
public static String get(String url) {
2023-02-27 20:48:09 +08:00
return http_get(url, null,false);
2021-12-06 11:19:00 +08:00
}
public static String post(final String url, final byte[] body) {
return http_post(url, body, 0, null);
}
2022-10-13 23:28:04 +08:00
public static File syncDownload(final String url, final String saveName,boolean isProxy) {
return new HttpTools().http_syncDownload(url, saveName,isProxy);
2021-12-06 11:19:00 +08:00
}
public static String https_get(String url, Map<String, String> header) {
try {
2022-02-08 12:27:07 +08:00
HttpsURLConnection connection;
connection = (HttpsURLConnection) new URL(url).openConnection();
return urlConnection(header, connection);
} catch (Exception e) {
System.err.println("error url = " + url);
e.printStackTrace();
}
return null;
}
2023-02-27 20:48:09 +08:00
public static String http_get(String url, Map<String, String> header,boolean isProxy) {
2022-02-08 12:27:07 +08:00
try {
HttpURLConnection connection;
2023-02-27 20:48:09 +08:00
Proxy proxy = null;
if (isProxy) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
}
if(proxy==null) {
connection = (HttpURLConnection) new URL(url).openConnection();
}else{
connection = (HttpURLConnection) new URL(url).openConnection(proxy);
}
2022-02-08 12:27:07 +08:00
return urlConnection(header, connection);
2021-12-06 11:19:00 +08:00
} catch (Exception e) {
System.err.println("error url = " + url);
e.printStackTrace();
}
return null;
}
2022-02-08 12:27:07 +08:00
@NotNull
private static String urlConnection(Map<String, String> header, HttpURLConnection connection) throws IOException {
connection.setRequestProperty("User-Agent", getExtUa());
if (header != null) {
for (String key : header.keySet()) {
connection.addRequestProperty(key, header.get(key));
}
}
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder str = new StringBuilder();
String tmp;
while ((tmp = reader.readLine()) != null) {
str.append(tmp);
2022-03-11 21:07:06 +08:00
str.append("\n");
2022-02-08 12:27:07 +08:00
}
reader.close();
return str.toString();
}
2021-12-06 11:19:00 +08:00
public static String http_post(String url, byte[] body, int index, Map<String, String> headers) {
String tmp;
StringBuilder str = new StringBuilder();
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("POST");
if (headers != null) {
for (String key : headers.keySet()) {
connection.addRequestProperty(key, headers.get(key));
}
}
connection.setDoOutput(true);
connection.setDoInput(true);
connection.addRequestProperty("User-Agent", getExtUa());
connection.setConnectTimeout(5 * 1000);
connection.setReadTimeout(10 * 1000);
//connection.addRequestProperty("Connection", "keep-alive");
//connection.addRequestProperty("User-Agent", getExtUa());
//connection.addRequestProperty("content-type", "application/json");
connection.addRequestProperty("charset", "UTF-8");
if (body != null) {
2022-04-08 02:26:29 +08:00
OutputStream outputStream = connection.getOutputStream();
//System.out.println(new String(body));
outputStream.write(body);
outputStream.flush();
outputStream.close();
}
2021-12-06 11:19:00 +08:00
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((tmp = reader.readLine()) != null) {
str.append(tmp);
}
String finalStr = str.toString();
connection.disconnect();
reader.close();
return finalStr;
} catch (Exception e) {
if (index < HttpRequestIndex) {
return http_post(url, body, index + 1, headers);
} else {
e.printStackTrace();
return null;
}
}
}
private static String getExtUa() {
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36";
}
public static String toUrlParams(JSONObject json) {
2021-12-06 11:19:00 +08:00
StringBuilder string = new StringBuilder();
Set<String> keys = json.keySet();
for (String key : keys) {
try {
2022-04-08 02:26:29 +08:00
string.append("&").append(key).append("=").append(URLEncoder.encode(json.getString(key), StandardCharsets.UTF_8));
2021-12-06 11:19:00 +08:00
} catch (Exception e) {
e.printStackTrace();
try {
2022-04-08 02:26:29 +08:00
string.append("&").append(URLEncoder.encode(key, StandardCharsets.UTF_8)).append("=");
2021-12-06 11:19:00 +08:00
// string += "&" + key + "=";
} catch (Exception e1) {
string.append("&").append(key).append("=");
}
}
}
string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", ""));
return string.toString();
}
2022-11-10 20:29:49 +08:00
public static String toUrlParams(Map<String,String> map) {
StringBuilder builder=new StringBuilder();
for (String key : map.keySet()) {
builder.append(key).append("=").append(map.get(key)).append("&");
}
return builder.substring(0, builder.length() - 1);
}
public static Map<String, String> getUrlParams(String url) {
Map<String, String> map = new HashMap<>();
if (url.contains("?")) {
String param = url.split("\\?")[1];
String[] params = param.split("&");
2022-04-08 02:26:29 +08:00
for (String par : params) {
map.put(par.split("=")[0], par.split("=")[1]);
2022-04-08 02:26:29 +08:00
}
}
return map;
}
2021-12-06 11:19:00 +08:00
public static void main(String[] args) {
2022-10-13 23:28:04 +08:00
File file = syncDownload("https://lain.bgm.tv/pic/cover/l/6c/2a/302128_qQIjG.jpg", "12345.jpg",false);
System.out.println("file.length() = " + file.length());
2021-12-06 11:19:00 +08:00
}
2022-04-17 01:20:40 +08:00
public static String downloadPath = "tmp" + File.separator;
2021-12-06 11:19:00 +08:00
public synchronized static void download(final String url, final String saveName, final DownloadInterface downloadInterface) {
download(url, saveName, false, downloadInterface);
2022-04-17 01:06:31 +08:00
}
public synchronized static void download(final String url, final String saveName, boolean isProxy, final DownloadInterface downloadInterface) {
2021-12-06 11:19:00 +08:00
new Thread(new Runnable() {
@Override
public void run() {
File jar = null;
try {
2022-04-17 01:20:40 +08:00
File savePath = new File(downloadPath);
Proxy proxy = null;
2021-12-06 11:19:00 +08:00
if (!savePath.exists()) {
savePath.mkdirs();
}
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
if (isProxy) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
2022-04-17 01:06:31 +08:00
}
2022-01-02 00:14:27 +08:00
URLConnection connection;
if (url.startsWith("https:")) {
if (isProxy) {
2022-04-17 01:06:31 +08:00
connection = (HttpsURLConnection) new URL(url).openConnection(proxy);
} else {
2022-04-17 01:06:31 +08:00
connection = (HttpsURLConnection) new URL(url).openConnection();
}
} else {
if (isProxy) {
2022-04-17 01:06:31 +08:00
connection = (HttpURLConnection) new URL(url).openConnection(proxy);
} else {
2022-04-17 01:06:31 +08:00
connection = (HttpURLConnection) new URL(url).openConnection();
}
2022-01-02 00:14:27 +08:00
}
2021-12-06 11:19:00 +08:00
connection.addRequestProperty("User-Agent", getExtUa());
// Log.i("获取到网络请求:"+((HttpsURLConnection)connection).getResponseCode());
2021-12-06 11:19:00 +08:00
InputStream inputStream = connection.getInputStream();
long fileSize = inputStream.available();
jar = new File(downloadPath + saveName + "_tmp.tmp 文件大小:" + fileSize);
2021-12-06 11:19:00 +08:00
jar.createNewFile();
Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
OutputStream outputStream = new FileOutputStream(jar);
byte[] bytes = new byte[1024];
double size = connection.getContentLength();
double downSize = 0;
int len;
while ((len = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, len);
downSize += len;
if (downloadInterface != null) {
downloadInterface.onDownloading(downSize, size);
}
}
outputStream.close();
inputStream.close();
2022-04-17 01:20:40 +08:00
File oldJar = new File(downloadPath + saveName);
2021-12-06 11:19:00 +08:00
if (oldJar.exists()) {
oldJar.delete();
}
jar.renameTo(oldJar);
Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName());
if (downloadInterface != null) {
if (oldJar.exists()) {
downloadInterface.onDownload(oldJar);
}else{
downloadInterface.onError(new FileNotFoundException("文件下载失败, 网络大小 = "+fileSize+" 本地大小 = "+oldJar.length()));
}
2021-12-06 11:19:00 +08:00
}
} catch (Exception e) {
e.printStackTrace();
if (jar != null) {
jar.delete();
}
if (downloadInterface != null) {
downloadInterface.onError(e);
}
}
}
}).start();
}
2022-10-13 23:28:04 +08:00
public synchronized File http_syncDownload(final String url, final String saveName,boolean isProxy) {
2021-12-06 11:19:00 +08:00
if (StringUtils.isEmpty(url)) {
return null;
}
File jar = null;
try {
2022-04-17 01:20:40 +08:00
File savePath = new File(downloadPath);
2021-12-06 11:19:00 +08:00
if (!savePath.exists()) {
savePath.mkdirs();
}
2022-10-13 23:28:04 +08:00
Proxy proxy = null;
if (isProxy) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
}
2021-12-06 11:19:00 +08:00
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
2022-10-13 23:28:04 +08:00
HttpURLConnection connection;
if(proxy==null) {
connection = (HttpURLConnection) new URL(url).openConnection();
}else{
connection = (HttpURLConnection) new URL(url).openConnection(proxy);
}
2021-12-06 11:19:00 +08:00
connection.addRequestProperty("User-Agent", getExtUa());
// Log.i(TAG,"获取到网络请求:"+connection.getResponseCode());
connection.addRequestProperty("Content-type", "image/jpeg");
2021-12-06 11:19:00 +08:00
InputStream inputStream = connection.getInputStream();
2022-04-17 01:20:40 +08:00
jar = new File(downloadPath + saveName + "_tmp.tmp");
2021-12-06 11:19:00 +08:00
jar.createNewFile();
Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
OutputStream outputStream = new FileOutputStream(jar);
byte[] bytes = new byte[1024];
double downSize = 0;
int len;
while ((len = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, len);
downSize += len;
}
outputStream.close();
inputStream.close();
2022-04-17 01:20:40 +08:00
File oldJar = new File(downloadPath + saveName);
2021-12-06 11:19:00 +08:00
if (oldJar.exists()) {
oldJar.delete();
}
connection.disconnect();
jar.renameTo(oldJar);
Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName());
return oldJar;
} catch (Exception e) {
e.printStackTrace();
if (jar != null) {
jar.delete();
}
return null;
}
}
public static String getLocalMacAddress() {
try {
InetAddress address = InetAddress.getLocalHost();
byte[] bytes = NetworkInterface.getByInetAddress(address).getHardwareAddress();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
if (i != 0) {
builder.append(":");
}
String tmp = Integer.toHexString(bytes[i] & 0xFF);
builder.append(tmp.length() == 1 ? 0 + tmp : tmp);
}
return builder.toString();
} catch (UnknownHostException | java.net.SocketException e) {
e.printStackTrace();
return null;
}
}
2021-12-06 11:19:00 +08:00
}