Код: Выделить всё
public static String get(String urlString, Map headers) {
HttpURLConnection urlConnection;
StringBuilder sb = new StringBuilder();
String jsonString = null;
URL url = null;
try {
url = new URL(urlString);
System.out.println(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
if (url != null) {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(10000);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
if (headers != null && !headers.isEmpty()) {
for (Map.Entry entry : headers.entrySet()) {
urlConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
jsonString = sb.toString();
if (DeviceUtils.isEmulator())
System.out.println("JSON: " + jsonString);
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonString;
}
Я пытался использовать get( "https://lemmy.world", ноль); чтобы получить html-страницу, но он дает мне 404 без вывода строки, в то время как другие сайты дают мне html-страницу
Подробнее здесь: https://stackoverflow.com/questions/789 ... some-sites
Мобильная версия