【问题标题】:How to get a video flow with Elixir/HTTPoison or Hackney?如何使用 Elixir/HTTPoison 或 Hackney 获得视频流?
【发布时间】:2017-01-07 16:16:34
【问题描述】:

我正在尝试使用 Elixir / HTTPoison 从摄像头获取流并将其转储到文件中。 网址为 x.x.x.x/axis-cgi/mjpg/video.cgi?duration=1&resolution=320x240

使用 DHC(Chrome 插件)中的 url,我可以拥有以下内容:

响应 200 OK

HEADERS Cache-Control: no-cache Pragma: no-cache Expires: Thu, 01 Dec 1994 16:00:00 GMT 连接:关闭内容类型: 多部分/x-混合替换;边界=myboundary X-Time-Offset: 62044.312573 相当完整的请求标头接受:/ 接受编码:gzip、deflate、sdch 接受语言:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 内容类型:application/xml 用户代理:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 注意:XHR 自动添加 Accept、Accept-Language、Cookie、User-Agent 等标头。

原始格式的正文向我展示了很多由 --myboundary 东西分隔的二进制 jpeg 图像。

--我的边界

内容类型:图片/jpeg

内容长度:11848

����JFIF��W�u]7W�u]7�� �2�̎TY"��C

现在,我想尝试用 Elixir 获得同样的效果。

  test "request headers from cam" do
    options = [hackney: [basic_auth: {"login","password"}]]
    {:ok, %HTTPoison.AsyncResponse{id: id}} = HTTPoison.get "http://x.x.x.x/axis-cgi/mjpg/video.cgi?duration=1&resolution=320x240",[], [stream_to: self, recv_timeout: :infinity, hackney: options]
    assert_receive %HTTPoison.AsyncStatus{id: ^id, code: 200}, 1_000
  end

我确实有以下回应,

  1) test request headers from cam (NewsequenceTest)
     test/newsequence_test.exs:9
     No message matching %HTTPoison.AsyncStatus{id: ^id, code: 200} after 1000ms.
     The following variables were pinned:
       id = #Reference<0.0.3.125>
     Process mailbox:
       %HTTPoison.AsyncStatus{code: 401, id: #Reference<0.0.3.125>}
       %HTTPoison.AsyncHeaders{headers: [{"Date", "Wed, 31 Aug 2016 06:47:00 GMT"}, {"Accept-Ranges", "bytes"}, {"Connection", "close"}, {"WWW-Authenticate", "Digest realm=\"AXIS_ACCC8E545922\", nonce=\"0000fa64Y50235919055a42f91d3dea744b10d2dab9653\", stale=FALSE, qop=\"auth\""}, {"WWW-Authenticate", "Basic realm=\"AXIS_ACCC8E545922\""}, {"Content-Length", "189"}, {"Content-Type", "text/html; charset=ISO-8859-1"}], id: #Reference<0.0.3.125>}
       %HTTPoison.AsyncChunk{chunk: "<HTML><HEAD><TITLE>401 Unauthorized</TITLE></HEAD>\n<BODY><H1>401 Unauthorized</H1>\nYour client does not have permission to get URL /axis-cgi/mjpg/video.cgi from this server.\n</BODY></HTML>\n", id: #Reference<0.0.3.125>}
       %HTTPoison.AsyncEnd{id: #Reference<0.0.3.125>}
     stacktrace:
       test/newsequence_test.exs:12: (test)

我确实尝试过不使用基本身份验证,但结果相同。

我还不明白这个身份验证错误发生了什么。 当然我误解了一些东西,有人可以请教我这个问题吗?

亲切的问候,

皮埃尔

【问题讨论】:

  • 试试options = [basic_auth: {"login","password"}]
  • 完美!谢谢多伯特!关于如何继续正确地将所有块放入文件的任何提示?
  • 以防万一,我确实找到了以下内容来获取文件。再次感谢多伯特。 gitlab.amherst.edu/…

标签: elixir multipart-mixed-replace httpoison


【解决方案1】:

所以这里的问题是代码正在调用:

HTTPoison.get(..., [], [..., hackney: [hackney: [basic_auth: {"login","password"}]]])

代替:

HTTPoison.get(..., [], [..., hackney: [basic_auth: {"login","password"}]])

这导致hackney 在尝试获取opts[:hackney][:basic_auth] 等时忽略它们,而这些都是nil

解决方法是替换:

options = [hackney: [basic_auth: {"login","password"}]]

options = [basic_auth: {"login","password"}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多