2026-02-26 20:18:57 +08:00
|
|
|
package com.coloryr.allmusic.server.netapi;
|
2020-02-20 23:22:58 +08:00
|
|
|
|
2024-04-19 09:46:54 +08:00
|
|
|
import com.coloryr.allmusic.server.core.AllMusic;
|
2026-02-26 20:18:57 +08:00
|
|
|
import com.coloryr.allmusic.server.core.music.MusicHttpClient;
|
2024-04-19 09:46:54 +08:00
|
|
|
import com.coloryr.allmusic.server.core.objs.HttpResObj;
|
2026-02-26 20:18:57 +08:00
|
|
|
import com.coloryr.allmusic.server.netapi.obj.EncResObj;
|
2021-06-09 01:38:40 +08:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
|
import com.google.gson.JsonObject;
|
2026-01-01 11:57:39 +08:00
|
|
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
|
|
|
|
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
|
|
|
|
import org.apache.hc.client5.http.cookie.Cookie;
|
|
|
|
|
import org.apache.hc.client5.http.cookie.CookieStore;
|
|
|
|
|
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
|
|
|
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
|
|
|
|
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
|
|
|
|
import org.apache.hc.core5.http.HttpEntity;
|
|
|
|
|
import org.apache.hc.core5.http.NameValuePair;
|
|
|
|
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
|
|
|
|
import org.apache.hc.core5.http.message.BasicNameValuePair;
|
2020-02-20 23:22:58 +08:00
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.InputStream;
|
2026-01-01 11:57:39 +08:00
|
|
|
import java.net.URI;
|
2020-03-02 12:15:58 +08:00
|
|
|
import java.net.URLEncoder;
|
2020-02-20 23:22:58 +08:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2022-01-03 15:27:38 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2020-02-20 23:22:58 +08:00
|
|
|
|
2026-02-26 20:18:57 +08:00
|
|
|
public class NetApiHttpClient {
|
2025-06-23 13:15:56 +08:00
|
|
|
|
2023-01-06 18:15:10 +08:00
|
|
|
public static HttpResObj get(String path, String data) {
|
2020-02-20 23:22:58 +08:00
|
|
|
try {
|
2026-03-04 13:57:59 +08:00
|
|
|
data = URLEncoder.encode(data, StandardCharsets.UTF_8);
|
2026-01-01 11:57:39 +08:00
|
|
|
HttpGet request = new HttpGet(path + data);
|
|
|
|
|
request.setHeader("referer", "https://music.163.com");
|
|
|
|
|
request.setHeader("content-type", "application/json;charset=UTF-8");
|
2026-02-26 20:18:57 +08:00
|
|
|
request.setHeader("user-agent", MusicHttpClient.UserAgent);
|
2026-01-01 11:57:39 +08:00
|
|
|
HttpClientContext context = HttpClientContext.create();
|
2026-02-26 20:18:57 +08:00
|
|
|
CookieStore cookieStore = MusicHttpClient.createCookieStore();
|
2026-01-01 11:57:39 +08:00
|
|
|
context.setCookieStore(cookieStore);
|
2026-02-26 20:18:57 +08:00
|
|
|
try (CloseableHttpResponse response = MusicHttpClient.client.execute(request, context)) {
|
2026-01-01 11:57:39 +08:00
|
|
|
int httpCode = response.getCode();
|
|
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
|
|
if (entity == null) {
|
2026-03-04 13:57:59 +08:00
|
|
|
AllMusic.log.warning("<light_purple>[AllMusic3]<red>获取网页错误");
|
2026-01-01 11:57:39 +08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
InputStream inputStream = entity.getContent();
|
|
|
|
|
boolean ok = httpCode == 200;
|
|
|
|
|
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
|
int length;
|
|
|
|
|
while ((length = inputStream.read(buffer)) != -1) {
|
|
|
|
|
result.write(buffer, 0, length);
|
|
|
|
|
}
|
|
|
|
|
inputStream.close();
|
|
|
|
|
EntityUtils.consume(entity);
|
2026-03-04 13:57:59 +08:00
|
|
|
String data1 = result.toString(StandardCharsets.UTF_8);
|
2026-01-01 11:57:39 +08:00
|
|
|
if (!ok) {
|
2026-03-04 13:57:59 +08:00
|
|
|
AllMusic.log.warning("<light_purple>[AllMusic3]<red>服务器返回错误:" + data1);
|
2026-01-01 11:57:39 +08:00
|
|
|
}
|
|
|
|
|
// 保存 cookies
|
2026-02-26 20:18:57 +08:00
|
|
|
MusicHttpClient.saveCookies(cookieStore);
|
2026-01-01 11:57:39 +08:00
|
|
|
return new HttpResObj(data1, ok);
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
2026-03-04 13:57:59 +08:00
|
|
|
AllMusic.log.warning("<light_purple>[AllMusic3]<red>获取网页错误");
|
2021-06-08 23:28:03 +08:00
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 18:15:10 +08:00
|
|
|
public static HttpResObj post(String url, JsonObject data, EncryptType type, String ourl) {
|
2021-06-08 23:28:03 +08:00
|
|
|
try {
|
2026-01-01 11:57:39 +08:00
|
|
|
HttpPost request = new HttpPost(url);
|
|
|
|
|
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
request.setHeader("Referer", "https://music.163.com");
|
|
|
|
|
HttpClientContext context = HttpClientContext.create();
|
2026-02-26 20:18:57 +08:00
|
|
|
CookieStore cookieStore = MusicHttpClient.createCookieStore();
|
2026-01-01 11:57:39 +08:00
|
|
|
context.setCookieStore(cookieStore);
|
2023-01-06 18:15:10 +08:00
|
|
|
EncResObj res;
|
2026-01-01 11:57:39 +08:00
|
|
|
List<Cookie> cookies = cookieStore.getCookies();
|
|
|
|
|
// 注意:这里需要根据域名过滤,但为了简化,我们使用所有 cookies
|
2022-01-03 15:27:38 +08:00
|
|
|
if (type == EncryptType.WEAPI) {
|
2026-02-26 20:18:57 +08:00
|
|
|
request.setHeader("User-Agent", MusicHttpClient.UserAgent);
|
2021-06-08 23:28:03 +08:00
|
|
|
String csrfToken = "";
|
2026-01-01 11:57:39 +08:00
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
if (cookie.getName().equalsIgnoreCase("__csrf")) {
|
|
|
|
|
csrfToken = cookie.getValue();
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
2022-01-03 15:27:38 +08:00
|
|
|
}
|
2021-06-09 01:38:40 +08:00
|
|
|
data.addProperty("csrf_token", csrfToken);
|
|
|
|
|
res = CryptoUtil.weapiEncrypt(AllMusic.gson.toJson(data));
|
2021-06-08 23:28:03 +08:00
|
|
|
url = url.replaceFirst("\\w*api", "weapi");
|
2026-01-01 11:57:39 +08:00
|
|
|
request.setUri(new URI(url));
|
|
|
|
|
List<NameValuePair> params = new ArrayList<>();
|
|
|
|
|
params.add(new BasicNameValuePair("params", res.params));
|
|
|
|
|
params.add(new BasicNameValuePair("encSecKey", res.encSecKey));
|
|
|
|
|
request.setEntity(new UrlEncodedFormEntity(params));
|
2022-01-03 15:27:38 +08:00
|
|
|
} else if (type == EncryptType.EAPI) {
|
2026-01-01 11:57:39 +08:00
|
|
|
request.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 9; PCT-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.64 HuaweiBrowser/10.0.3.311 Mobile Safari/537.36");
|
2021-06-09 01:38:40 +08:00
|
|
|
JsonObject header = new JsonObject();
|
2023-11-23 21:08:17 +08:00
|
|
|
header.addProperty("appver", "8.10.90");
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("versioncode", "140");
|
|
|
|
|
header.addProperty("buildver", new Date().toString().substring(0, 10));
|
|
|
|
|
header.addProperty("resolution", "1920x1080");
|
|
|
|
|
header.addProperty("os", "android");
|
2021-06-08 23:28:03 +08:00
|
|
|
String requestId = "0000" + (new Date() + "_" + Math.floor(Math.random() * 1000));
|
2021-06-09 01:38:40 +08:00
|
|
|
header.addProperty("requestId", requestId);
|
2026-01-01 11:57:39 +08:00
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
if (cookie.getName().equalsIgnoreCase("MUSIC_U")) {
|
|
|
|
|
header.addProperty("MUSIC_U", cookie.getValue());
|
|
|
|
|
} else if (cookie.getName().equalsIgnoreCase("MUSIC_A")) {
|
|
|
|
|
header.addProperty("MUSIC_A", cookie.getValue());
|
|
|
|
|
} else if (cookie.getName().equalsIgnoreCase("channel")) {
|
|
|
|
|
header.addProperty("channel", cookie.getValue());
|
|
|
|
|
} else if (cookie.getName().equalsIgnoreCase("mobilename")) {
|
|
|
|
|
header.addProperty("mobilename", cookie.getValue());
|
|
|
|
|
} else if (cookie.getName().equalsIgnoreCase("osver")) {
|
|
|
|
|
header.addProperty("osver", cookie.getValue());
|
|
|
|
|
} else if (cookie.getName().equalsIgnoreCase("__csrf")) {
|
|
|
|
|
header.addProperty("__csrf", cookie.getValue());
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
|
|
|
|
}
|
2021-06-09 01:38:40 +08:00
|
|
|
data.add("header", header);
|
2021-06-08 23:28:03 +08:00
|
|
|
res = CryptoUtil.eapi(ourl, data);
|
|
|
|
|
url = url.replaceFirst("\\w*api", "eapi");
|
2026-01-01 11:57:39 +08:00
|
|
|
request.setUri(new URI(url));
|
|
|
|
|
List<NameValuePair> params = new ArrayList<>();
|
|
|
|
|
params.add(new BasicNameValuePair("params", res.params));
|
|
|
|
|
request.setEntity(new UrlEncodedFormEntity(params));
|
2022-01-03 15:27:38 +08:00
|
|
|
} else {
|
2026-01-01 11:57:39 +08:00
|
|
|
request.setUri(new URI(url));
|
|
|
|
|
List<NameValuePair> params = new ArrayList<>();
|
2021-06-09 01:38:40 +08:00
|
|
|
for (Map.Entry<String, JsonElement> item : data.entrySet()) {
|
2026-01-01 11:57:39 +08:00
|
|
|
params.add(new BasicNameValuePair(item.getKey(), item.getValue().getAsString()));
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
2026-01-01 11:57:39 +08:00
|
|
|
request.setEntity(new UrlEncodedFormEntity(params));
|
2021-06-08 23:28:03 +08:00
|
|
|
}
|
2026-02-26 20:18:57 +08:00
|
|
|
try (CloseableHttpResponse response = MusicHttpClient.client.execute(request, context)) {
|
2026-01-01 11:57:39 +08:00
|
|
|
int httpCode = response.getCode();
|
|
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
|
|
if (entity == null) {
|
2026-03-04 13:57:59 +08:00
|
|
|
AllMusic.log.warning("<light_purple>[AllMusic3]<red>获取网页错误");
|
2026-01-01 11:57:39 +08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
InputStream inputStream = entity.getContent();
|
|
|
|
|
boolean ok = httpCode == 200;
|
|
|
|
|
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
|
int length;
|
|
|
|
|
while ((length = inputStream.read(buffer)) != -1) {
|
|
|
|
|
result.write(buffer, 0, length);
|
|
|
|
|
}
|
2026-03-04 13:57:59 +08:00
|
|
|
String data1 = result.toString(StandardCharsets.UTF_8);
|
2026-01-01 11:57:39 +08:00
|
|
|
EntityUtils.consume(entity);
|
|
|
|
|
if (!ok) {
|
2026-03-04 13:57:59 +08:00
|
|
|
AllMusic.log.warning("<light_purple>[AllMusic3]<red>服务器返回错误:" + data1);
|
2026-01-01 11:57:39 +08:00
|
|
|
}
|
|
|
|
|
// 保存 cookies
|
2026-02-26 20:18:57 +08:00
|
|
|
MusicHttpClient.saveCookies(cookieStore);
|
2026-01-01 11:57:39 +08:00
|
|
|
return new HttpResObj(data1, ok);
|
2020-03-11 18:10:14 +08:00
|
|
|
}
|
2020-03-02 12:15:58 +08:00
|
|
|
} catch (Exception e) {
|
2026-03-04 13:57:59 +08:00
|
|
|
AllMusic.log.warning("<light_purple>[AllMusic3]<red>获取网页错误");
|
2020-02-20 23:22:58 +08:00
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|