3

Dart 中如何使用 HTTP 服务器完成 404 重定向?

伪代码:

server.addRequestHandler(notFound, redirectTo(...));

提前致谢。

4

1 回答 1

3

如果要进行URL 重定向,可以使用:

  • Refresh标题:
server.addRequestHandler(notFound, (HttpRequest request, HttpResponse response) {
  response.statusCode = HttpStatus.NOT_FOUND;
  response.headers.set("Refresh", "0; url=http://www.example.com/");
  response.outputStream.close();
});
  • 如果您想在重定向之前显示一些文本,则进行元刷新:
server.addRequestHandler(notFound, (HttpRequest request, HttpResponse response) {
  response.statusCode = HttpStatus.NOT_FOUND;
  response.headers.set(HttpHeaders.CONTENT_TYPE, 'text/html');
  response.outputStream.writeString(
      '<!DOCTYPE html>'
      '<meta http-equiv="Refresh" content="5; url=\'http://example.com/\'">'
      'Page requested cannot be found. You will be redirected in few seconds...'
  );
  response.outputStream.close();
});
于 2012-11-28T10:39:10.260 回答