chore(translate): more text for workaround
This commit is contained in:
parent
b47bc6fd0a
commit
5279dcb759
|
@ -11,6 +11,9 @@ import java.io.InputStreamReader;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -108,8 +111,22 @@ public class GoogleTranslateService extends AbstractTranslateService<GoogleTrans
|
||||||
return get(pageUrl);
|
return get(pageUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String get2(String url) {
|
||||||
|
// An attempt to use the stuff wrapped by HttpClient. Will need to test and (if it works) adjust commons so it can be used again
|
||||||
|
try {
|
||||||
|
HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build();
|
||||||
|
HttpResponse<String> response = client.send(HttpRequest.newBuilder().uri(new URI(url)).build(), HttpResponse.BodyHandlers.ofString());
|
||||||
|
if (response.statusCode() / 100 != 2) {
|
||||||
|
throw new IOException("Could not get page: " + response.statusCode());
|
||||||
|
}
|
||||||
|
return response.body();
|
||||||
|
} catch (URISyntaxException | IOException | InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static String get(String url) {
|
private static String get(String url) {
|
||||||
// Technically, we should be using HttpClient, but Google Translate doesn't like it for some reason
|
// Technically, we should be using HttpClient, but Google Translate doesn't like it for some reason and this mess bypasses that
|
||||||
// based on https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/helper/HttpConnection.java
|
// based on https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/helper/HttpConnection.java
|
||||||
try {
|
try {
|
||||||
HttpsURLConnection connection = (HttpsURLConnection) URI.create(url).toURL().openConnection();
|
HttpsURLConnection connection = (HttpsURLConnection) URI.create(url).toURL().openConnection();
|
||||||
|
@ -120,7 +137,10 @@ public class GoogleTranslateService extends AbstractTranslateService<GoogleTrans
|
||||||
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
|
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
|
||||||
connection.connect();
|
connection.connect();
|
||||||
if (connection.getResponseCode() / 100 != 2) {
|
if (connection.getResponseCode() / 100 != 2) {
|
||||||
throw new IOException("Could not get page: " + connection.getResponseCode());
|
String loc = connection.getHeaderField("Location");
|
||||||
|
if (loc == null) loc = "";
|
||||||
|
else loc = " (redirected to " + loc + ")";
|
||||||
|
throw new IOException("Could not get page: " + connection.getResponseCode() + loc);
|
||||||
}
|
}
|
||||||
connection.getRequestMethod();
|
connection.getRequestMethod();
|
||||||
connection.getURL();
|
connection.getURL();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user