我想檢查該網址是否存在。
功能:
Future _checkUrl(String url) async {
http.Response _urlResponse = await http.get(Uri.parse(url));
if (_urlResponse.statusCode == 200) {
return true;
}
else {
return false;
}
}
稱呼:
_checkUrl("https://stackoverf").then((value) => {
print(value)
});
它在我給出https://fonts.google.com/?category=Sans Serif(回傳真)或https://stackoverflow.com/qu(回傳假)時起作用。
但是當我嘗試使用https://stackoverf哪個無效時,它給了我[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: Failed host lookup: 'stackoverf' (OS Error: No address associated with hostname, errno = 7).
如何_checkUrl通過此呼叫使回傳錯誤?
uj5u.com熱心網友回復:
像這樣的東西會適合你想要的嗎?
Future<bool> _checkUrl(String url) async {
try {
http.Response _urlResponse = await http.get(Uri.parse(url));
return _urlResponse.statusCode == 200;
// return _urlResponse.statusCode < 400 // in case you want to accept 301 for example
} on SocketException {
return false;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383459.html
