修复B站登陆监测403问题

This commit is contained in:
Yutousama 2022-04-08 02:40:33 +08:00
parent 8145a1c313
commit c04ada610f
2 changed files with 6 additions and 5 deletions

View File

@ -17,7 +17,7 @@ public class BiliBiliUtils {
public synchronized static JSONObject http_get(String url) { public synchronized static JSONObject http_get(String url) {
try { try {
// Log.i("调用url = "+url); // Log.i("调用url = "+url);
HttpURLConnection connection = getBiliHttpGet(url, getCookie()); HttpsURLConnection connection = getBiliHttpGet(url, getCookie());
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream()); BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024]; byte[] bytes = new byte[1024];
@ -135,7 +135,7 @@ public class BiliBiliUtils {
return connection; return connection;
} }
public static HttpURLConnection getBiliHttpGet(String url, String cookie) throws IOException { public static HttpsURLConnection getBiliHttpGet(String url, String cookie) throws IOException {
if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) { if (System.currentTimeMillis() - oldBiliBiliHttpTime < 1000) {
try { try {
Thread.sleep(500); Thread.sleep(500);
@ -143,7 +143,7 @@ public class BiliBiliUtils {
} }
oldBiliBiliHttpTime = System.currentTimeMillis(); oldBiliBiliHttpTime = System.currentTimeMillis();
} }
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
setConnection(cookie, connection); setConnection(cookie, connection);
connection.setReadTimeout(5000); connection.setReadTimeout(5000);
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
@ -154,7 +154,7 @@ public class BiliBiliUtils {
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
connection.setRequestProperty("Cache-Control", "max-age=0"); connection.setRequestProperty("Cache-Control", "max-age=0");
connection.setRequestProperty("Referer", ".bilibili.com"); //connection.setRequestProperty("Referer", ".bilibili.com");
connection.setRequestProperty("Connection", "keep-alive"); connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Upgrade-Insecure-Requests", "1"); connection.setRequestProperty("Upgrade-Insecure-Requests", "1");
connection.setRequestProperty("Cookie", cookie); connection.setRequestProperty("Cookie", cookie);

View File

@ -68,6 +68,7 @@ public class BiliLogin {
public static void main(String[] args) { public static void main(String[] args) {
BiliLogin login = new BiliLogin(); BiliLogin login = new BiliLogin();
login.testLogin(); boolean testLogin = login.testLogin();
System.out.println("testLogin = " + testLogin);
} }
} }