Created
September 25, 2024 20:39
-
-
Save DJWOMS/56c16b630d156d2517a0272929d12e6a to your computer and use it in GitHub Desktop.
1) Программа которая проверяет доступность веб-сайта по его URL-адресу. 2) Программа которая скачивает файл с указанного URL-адреса и сохраняет его на диск.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:http/http.dart' as http; | |
void main() async { | |
checkSite('https://www.google.com'); | |
checkSite('https://www.nonexistentsite.com'); | |
} | |
void checkSite(String url) async { | |
final uri = Uri.parse(url); | |
try { | |
var response = await http.get(uri); | |
if (response.statusCode == 200) { | |
print('Сайт ${uri.host} доступен.'); | |
} else { | |
print('Сайт ${uri.host} недоступен.'); | |
} | |
} catch (e) { | |
if (e is SocketException) { | |
print('Сайт ${uri.host} недоступен: $e'); | |
} else { | |
rethrow; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'dart:math'; | |
import 'package:http/http.dart' as http; | |
void main() async { | |
downloadFile('https://royallib.com/get/txt/stivenson_nil/v_nachale_bila_komandnaya_stroka.zip'); | |
} | |
void downloadFile(String url) async { | |
final uri = Uri.parse(url); | |
var extension = url.split('.').last; | |
var fileName = Random().nextInt(1000).toString() + '.' + extension; | |
var file = File(fileName); | |
var response = await http.get(uri); | |
await file.writeAsBytes(response.bodyBytes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment