zoukankan      html  css  js  c++  java
  • httpc使用总结

    1、httpc支持stream下载文件

    参见erlang官方文档:

    stream

    Streams the body of a 200 or 206 response to the calling process or to a file. When streaming to the calling process using the option self the following stream messages will be sent to that process: {http, {RequestId, stream_start, Headers}, {http, {RequestId, stream, BinBodyPart}, {http, {RequestId, stream_end, Headers}. When streaming to to the calling processes using the option {self, once} the first message will have an additional element e.i. {http, {RequestId, stream_start, Headers, Pid}, this is the process id that should be used as an argument to http:stream_next/1 to trigger the next message to be sent to the calling process.

    Note that it is possible that chunked encoding will add headers so that there are more headers in the stream_end message than in the stream_start. When streaming to a file and the request is asynchronous the message {http, {RequestId, saved_to_file}} will be sent.

    Defaults to none.

    {ok, _RequestId} = httpc:request(get,{Url,[{"User-Agent", "RYT"}]}, [],[{sync, false}, {body_format, binary}, {stream, self}]),

    loop_recv() ->
        receive
            {http, {RequestId, stream_start, _Headers}} ->
                io:format("recv data ~p stream_start ~n",[RequestId]),
                loop_recv();
            {http, {_RequestId, stream, BinBodyPart}}->
                io:format("recv data ~p BinBodyPart: ~n~p~n",[RequestId,BinBodyPart]),
                loop_recv();
            {http, {RequestId, stream_end, _Headers}} ->
                io:format("recv data ~p stream_end  ~n",[RequestId]);
            R ->
                io:format("recv error data:  ~p~n",[R])
        end.

    {sync,false} 异步请求

    {body_format, binary} body为二进制格式

    {steam, self} 支持steam方式,且发送到请求进程

    loop_recv() 发送完异步下载请求后,循环等待接收,直到收到stream_end

  • 相关阅读:
    Sun:收购MySQL是现代软件史上最重要收购[ZT]
    SCI2012年收录的中文期刊
    Elsevier期刊投稿状态
    医学图像SCI
    贝叶斯法则,先验概率,后验概率,最大后验概率
    医学图像处理与分析方面的大牛
    2013 EI检索的国内期刊
    ICIP EMBC IUS 2013
    香港中文大学第六十九届颁授学位典礼 校长赠言 我默祷你们都能不负此生
    自动生成参考文献编号
  • 原文地址:https://www.cnblogs.com/yanwei-wang/p/5144603.html
Copyright © 2011-2022 走看看