【问题标题】:How to unset or remove the proxy option on Erlang httpc client default profile once set?设置后如何取消设置或删除 Erlang httpc 客户端默认配置文件上的代理选项?
【发布时间】:2019-07-27 05:14:49
【问题描述】:

代理原来没有设置,显示为未定义:

httpc:get_options(all).
{ok,[{proxy,{undefined,[]}},
     {https_proxy,{undefined,[]}},
     {pipeline_timeout,0},
     {max_pipeline_length,2},
     {max_keep_alive_length,5},
     {keep_alive_timeout,120000},
     {max_sessions,2},
     {cookies,disabled},
     {verbose,false},
     {ipfamily,inet},
     {ip,default},
     {port,default},
     {socket_opts,[]},
     {unix_socket,undefined}]}

我可以毫无问题地设置代理选项:

httpc:set_options([{proxy, {{"www-proxy.mycompany.com", 8000},["localhost"]}}]).

当不需要代理时,如何将代理取消设置为未定义(或没有代理)?我试过了:

httpc:set_options([{proxy,{undefined, []}}]).
But it throws an exception:
** exception throw: {error,{bad_option,proxy,{undefined,[]}}}
     in function  httpc:bad_option/2 (httpc.erl, line 1102)
     in call from httpc:validate_options/2 (httpc.erl, line 932)
     in call from httpc:validate_options/1 (httpc.erl, line 922)
     in call from httpc:set_options/2 (httpc.erl, line 236)

我做错了什么?

【问题讨论】:

  • 好吧,我想我总是可以使用:exit(whereis(httpc_manager), kill)。并让主管做一个新的。但希望有办法清除代理信息而不必诉诸于杀死它。

标签: erlang http-proxy httpc


【解决方案1】:

你做错了什么是你传递给函数的参数格式。正确的格式是

httpc:set_options([{proxy, {{"", 0},[]}}]).

现在代理主机将是 "":0。但我不知道你的任务是否可以接受。

对评论的回应: 尝试直接将 'proxy' 选项设置为 http_manager 而不是杀死他:

httpc_manager:set_options([{proxy,{undefined, []}}],httpc_manager).

看看erlang shell:

1> inets:start().
ok
2> httpc:set_options([{proxy, {{"www-proxy.mycompany.com", 8000},["localhost"]}}]).
ok
3> httpc:get_options(all).
{ok,[{proxy,{{"www-proxy.mycompany.com",8000},
             ["localhost"]}},
     {https_proxy,{undefined,[]}},
     {pipeline_timeout,0},
     {max_pipeline_length,2},
     {max_keep_alive_length,5},
     {keep_alive_timeout,120000},
     {max_sessions,2},
     {cookies,disabled},
     {verbose,false},
     {ipfamily,inet},
     {ip,default},
     {port,default},
     {socket_opts,[]},
     {unix_socket,undefined}]}
4> httpc_manager:set_options([{proxy,{undefined, []}}],httpc_manager).
ok
5> httpc:get_options(all).                                            
{ok,[{proxy,{undefined,[]}},
     {https_proxy,{undefined,[]}},
     {pipeline_timeout,0},
     {max_pipeline_length,2},
     {max_keep_alive_length,5},
     {keep_alive_timeout,120000},
     {max_sessions,2},
     {cookies,disabled},
     {verbose,false},
     {ipfamily,inet},
     {ip,default},
     {port,default},
     {socket_opts,[]},
     {unix_socket,undefined}]}

【讨论】:

  • 是的,查看代码,他们正在寻找 {{::list(), ::non_neg_integer()}, ::list()},就像您指出的那样。但不确定这个建议是否有效。是否等同于没有设置代理?它可能会尝试路由到任何地方。我会尝试一下,我们会看看会发生什么。很快就会回来报告。
  • 那行不通。我发现的唯一解决方法是在需要代理重置时使用 exit(whereis(httpc_manager),kill) 杀死 httpc 管理器。从未定义的代理设置开始。
  • 我在答案中添加了其他解决方案。现在可以用了吗?
  • 是的,成功了!我还必须为 https_proxy 添加重置。例如httpc_manager:set_options([{https_proxy,{undefined, []}}],httpc_manager).
猜你喜欢
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 2012-05-12
  • 1970-01-01
  • 2021-06-24
  • 2021-03-19
  • 1970-01-01
  • 2020-12-25
相关资源
最近更新 更多