3

尝试设置一个简单的 Dart HttpServer

import 'dart:io';

void main() {
  HttpServer.bind(InternetAddress.ANY_IP_V4, 80).then((server) {
    server.listen((HttpRequest request) {
      request.response.write('Hello, world.');
      request.response.close();
      print(new DateTime.now());
      print(request.connectionInfo.remoteAddress);
      print(request.method);
      print(request.headers.toString());
      print("--------------");      
    });
  });

  print("listing....");

}

当从浏览器(Chrome)点击 localhost 时,看起来好像传入的请求被处理了两次

listing....
2013-11-07 15:19:24.478
InternetAddress('127.0.0.1', IP_V4)
GET
host: localhost:80
connection: keep-alive
cache-control: max-age=0
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
accept-encoding: gzip,deflate,sdch
accept-language: en-US,en;q=0.8
--------------
2013-11-07 15:19:24.554
InternetAddress('127.0.0.1', IP_V4)
GET
host: localhost:80
connection: keep-alive
accept: */*
user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
accept-encoding: gzip,deflate,sdch
accept-language: en-US,en;q=0.8
--------------

除了标头之外,这两个请求看起来几乎相同accept。看起来浏览器没有两次触发请求:

enter image description here

那么,为什么请求会被处理两次?

编辑:Dart SDK 版本 0.8.10.6_r30036

4

1 回答 1

5

您不输出请求的内容(url请求实例的成员),这就是两个请求之间的区别。

The first request requests the file you try to open, probably /. The second request is issued internally by the browser and requests favicon.ico to display the icon in the address bar / tab title.

于 2013-11-08T05:17:31.000 回答