Google App Engine(Java):URL获取响应过大的问题

我正在尝试在谷歌应用程序上构建某种web服务。

现在问题是,我需要从网站获取数据(HTML Scraping)。

请求如下:

URL url = new URL(p_url); con = (HttpURLConnection) url.openConnection(); InputStreamReader in = new InputStreamReader(con.getInputStream()); BufferedReader reader = new BufferedReader(in); String result = ""; String line = ""; while((line = reader.readLine()) != null) { System.out.println(line); } return result; 

现在,App Engine在第3行给出了以下例外情况:

 com.google.appengine.api.urlfetch.ResponseTooLargeException 

这是因为最大请求限制为1mb,页面的总HTML大约为1.5mb。

现在我的问题是:我只需要html的前20行来刮。 有没有办法只获取HTML的一部分,以便不会抛出ResponseTooLargeException?

提前致谢!

通过使用低级URLFetch api解决了这个问题。

并将allowtruncate选项设置为true;

http://code.google.com/intl/nl-NL/appengine/docs/java/javadoc/com/google/appengine/api/urlfetch/FetchOptions.html

基本上它的工作原理如下:

 HTTPRequest request = new HTTPRequest(_url, HTTPMethod.POST, Builder.allowTruncate()); URLFetchService service = URLFetchServiceFactory.getURLFetchService(); HTTPResponse response = service.fetch(request);