swi prolog 8.0.2 : gziped http



我试图使工作成为打开HTTP连接的代码。尽管如此,网页可能会以纯文本或GZIPED传输。结果,具有实用主义的代码试图以纯文本打开,如果失败并收到异常,则会像编码GZIP一样尝试。

URL是唯一的地面变量。尝试使用url ='http://releases.llvm.org/6.0.0/tools/clang/docs/clangcommandlineereference.html'例如。

                user::catch(
                (
                 user::http_open(URL, DataStream, []),                            
                 user::load_html(stream(DataStream), Terms, []),
                 user::close(DataStream)
                ),
                _
                ,
                (
                 user::open_any(URL, read, GZipDataStream, CloseIt, [encoding(gzip), string(atom)]),
                 /*user::http:encoding_filter(gzip, DataStream, GZipDataStream),*/
                 user::load_html(stream(GZipDataStream), Terms, []),
                 user::close_any(CloseIt)
                )
                )

forport,捕获的恢复部分不起作用。

有什么建议?

目标中的user::前缀表明您发布的代码是logtalk的片段。如果是这样,它是滥用logtalk源代码并建立对SWI-Promog自动加载机制的依赖性。可以重写该代码以清晰度和弹性。这样做并修复其中的错误(必须加载library(zlib)才能使Avaialble使http:encoding_filter/3过滤器)在以下解决方案中结果:

:- use_module(library(http/http_open), []).
:- use_module(library(sgml), []).
:- use_module(library(iostream), []).
:- use_module(library(zlib), []).

:- object(html).
    :- public(get_url/2).
    % override ambiguous meta-predicate template
    :- meta_predicate(sgml:load_html(*,*,*)).
    get_url(URL, Terms) :-
        catch(
                setup_call_cleanup(
                    http:http_open(URL, DataStream, []),
                    sgml:load_html(stream(DataStream), Terms, []),
                    close(DataStream)
                ),
                _,
                setup_call_cleanup(
                    iostream:open_any(URL, read, DataStream, CloseIt, [string(atom)]),
                    sgml:load_html(stream(DataStream), Terms, []),
                    iostream:close_any(CloseIt)
                )
            ).
:- end_object.

setup_call_cleanup/3调用确保在错误时关闭打开的流。

假设上面的对象保存在html.lgt文件中,以下示例调用显示其适用于您发布的URL:

?- {html}.
...
% (0 warnings)
true.
?- html::get_url('http://releases.llvm.org/6.0.0/tools/clang/docs/ClangCommandLineReference.html', Terms).
Terms = [element(html, [xmlns='http://www.w3.org/1999/xhtml'], [element(head, [], [element(meta, ['http-equiv'='Content-Type', content='text/html; charset=utf-8'], []), element(title, [], ['Clang command line argument reference — Clang 6 documentation']), element(link, [... = ...|...], []), element(link, [...|...], []), element(..., ..., ...)|...]), element(body, [role=document], ['      ', element(div, [... = ...|...], [element(..., ..., ...)|...]), 'n      ', element(..., ..., ...)|...])])].

相关内容

  • 没有找到相关文章

最新更新