inets httpc: client example in User's Guide not working

安稳与你 提交于 2019-12-08 10:43:31

问题


I copied the following code from the inets User's Guide:

$ erl
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.2  (abort with ^G)

1> inets:start().
ok

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

3> {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} = httpc:request("http://www.erlang.org").
* exception error: no match of right hand side value 
                {error,
                    {failed_connect,
                        [{to_address,{"www-proxy.mycompany.com",8000}},
                         {inet,[inet],etimedout}]}}

I need advice on how to get that request to complete successfully.


回答1:


This seems to work:

$ erl
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.2  (abort with ^G)

1> inets:start().
ok

2> {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} = httpc:request("http://www.erlang.org").
{ok,{{"HTTP/1.1",200,"OK"},
     [{"connection","keep-alive"},
      {"date","Sun, 25 Feb 2018 13:44:59 GMT"},
      {"server","nginx"},
      {"vary","Accept-Encoding"},
      {"content-length","12816"},
      {"content-type","text/html; charset=UTF-8"}],
     "<!DOCTYPE html>\n<html>\n  <head>\n    <title>Erlang Programming Language</title>\n    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n    <meta name=\"description\" content=\"Erlang Programming Language\"/>\n    <meta name=\"keywords\" content=\"erlang, functional, programming, fault-tolerant, distributed, multi-platform, portable, software, multi-core, smp, concurrency\"/>\n  

The User's Guide says:

The following calls use the default client profile. Use the proxy "www-proxy.mycompany.com:8000", except from requests to localhost. This applies to all the following requests.

which makes as much sense to me as:

Maecenas in tincidunt diam. Pellentesque in orci sed dolor vulputate tincidunt. Etiam malesuada finibus nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

To stop the client, do I just do:

  inets:stop()

?

Edit: I must have figured out how to stop the server because I did it in this code:

Erlang read post request



来源:https://stackoverflow.com/questions/48973601/inets-httpc-client-example-in-users-guide-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!