2013-12-11 4 views
2

클라이언트에서 URL (dart:html)을 가져 오는 것이 straightforward 인 경우 서버 측 (dart:io)에는 편리한 getString 메소드가 없습니다.다트 서버 (다트 : io)에서 URL을 가져 오는 방법은 무엇입니까?

간단히 URL 문서를 문자열로로드하는 방법은 무엇입니까?

import "dart:io"; 
import "dart:async"; 
import "dart:convert"; 

Future<String> fetch(String url) { 
    var completer = new Completer(); 
    var client = new HttpClient(); 
    client.getUrl(Uri.parse(url)) 
    .then((request) { 
     // Just call close on the request to send it. 
     return request.close(); 
    }) 
    .then((response) { 
     // Process the response through the UTF-8 decoder. 
     response.transform(const Utf8Decoder()).join().then(completer.complete); 
    }); 
    return completer.future; 
} 

당신은이 같은이 방법/기능을 사용합니다 :

답변

3

를 사용하여 작업을해야하는 문자열로 응답 본문을 반환 http 패키지와 read 기능 :이 작업을 수행하는 HTTP 패키지의 사용을 추천 할 것입니다

import 'package:http/http.dart' as http; 

void main() { 
    http.read("http://httpbin.org/").then(print); 
} 
0

이 도움이 될 것입니다이 일을 얻을 수

fetch("http://www.google.com/").then(print); 

을하지만,이 하지 A는 점에 유의하시기 바랍니다 강력한 솔루션. 다른 한편, 명령 행 스크립트보다 더 많은 일을한다면 어쨌든 이보다 더 많은 것을 필요로 할 것입니다.

+0

. 그것은 사물을 크게 단순화합니다. –

1

이 서버

import 'package:http/http.dart' as http; 

void main(List<String> args) { 
    http.get("http://www.google.com").then((http.Response e) => print(e.statusCode)); 
}