Archive

Posts Tagged ‘proxy’

HTTP request through proxy in Erlang

July 5, 2010 Leave a comment

Last week I was testing at university  the code I used in the post XML and Erlang. Oddly the first few lines regarding the http request to the direct messages fell into an error. This seemed very strange, because testing the code at home everything was working fine.

The code was more or less the following:

receive_msg(User, Pwd) ->
    application:start(inets),
    Aut = make_auth(User, Pwd),
    http:request(get, {"http://twitter.com/direct_messages.xml",
		       [{"Authorization", Aut}]}, [], []).

After a while I came to the solution: at the university there is a proxy server!

In Erlang, if you are behind a proxy server, all of your requests should specify the proxy address and port, here is the previous function modified to support connection through proxy server:

receive_msg(User, Pwd, Proxy, Port) ->
    application:start(inets),
    http:set_options([{proxy, {{Proxy, Port}, ["localhost"]}}]),
    Aut = make_auth(User, Pwd),
    http:request(get, {"http://twitter.com/direct_messages.xml",
		       [{"Authorization", Aut}]}, [], []).

In the previous lines Proxy is the proxy addres, in my case “proxy.university.it” and Port is simply the port number.

After this correction the code was working fine and I could go further with the testing!

Categories: English, Erlang Tags: , ,