修复B站无法登陆问题

This commit is contained in:
2024-01-08 17:31:21 +08:00
parent 177c91c370
commit 3437a5386f
2 changed files with 86 additions and 41 deletions

View File

@@ -71,6 +71,54 @@ public class BiliBiliUtils {
return null;
}
public JSONObject http_login_getSid(String url) {
try {
Log.i("调用url = "+url);
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
setConnection(null,connection);
connection.setRequestMethod("GET");
connection.connect();
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = stream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
outputStream.flush();
}
List<String> cookie = null;
if (connection.getHeaderFields().containsKey("Set-Cookie")) {
cookie = connection.getHeaderFields().get("Set-Cookie");
}
String str = outputStream.toString(StandardCharsets.UTF_8);
outputStream.close();
try {
JSONObject json = JSON.parseObject(str);
if(cookie!=null){
StringBuilder ck=new StringBuilder();
for (String key : cookie) {
ck.append(";").append(key);
}
json.put("cookie",ck.toString());
}
return json;
} catch (Exception e) {
JSONObject json = new JSONObject();
json.put("html", str);
return json;
} finally {
stream.close();
connection.disconnect();
}
} catch (IOException e) {
//com.yutou.bilibili.Tools.Log.e(e);
e.printStackTrace();
}
return null;
}
public JSONObject http_post(String url, String body) {
return http(url, HTTP.POST, body, RET_MODEL.JSON);
}
@@ -318,7 +366,9 @@ public class BiliBiliUtils {
connection.setRequestProperty("Referer", "https://www.bilibili.com");
connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("Upgrade-Insecure-Requests", "1");
connection.addRequestProperty("Cookie", cookie);
if(!StringUtils.isEmpty(cookie)) {
connection.addRequestProperty("Cookie", cookie);
}
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36");
}