从Python httplib到Erlang httpc

时间:2012-07-14 17:12:00

标签: python erlang httplib

我想从Python中的代码切换到Erlang:

的Python:

import httplib

body="<xml>...</xml>"
headers = { \
"Accept" :  "text/*" , \
"User-Agent" : "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" , \
"Host" : "something.com" , \
"Content-Length" : str( len( body ) ) , \
"Connection" : "Keep-Alive" , \
"Cache-Control" : "no-cache" , \
}
server="something.com"
url="/s.srf"

conn = httplib.HTTPSConnection(server,443)
conn.request("POST", url, body, headers)

- &GT;我试过这个Erlang:

Headers=[
        {"Accept","text/*"},
        {"User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"},
        {"Host","something.com"},
        {"Content-Length",string:len(Body)},
        {"Connection","Keep-Alive"},
        {"Cache-Control","no-cache"}
        ].
httpc:request(post,{Server,Headers,[],Body},[],[]).

但我不知道在哪里可以把网址“/s.srf”,任何想法?

2 个答案:

答案 0 :(得分:0)

request() tuple中的第一个元素应该是整个网址,而不仅仅是服务器:

Url = "http://" ++ Server ++ "/s.srf",
httpc:request(post,{Url,Headers,[],Body},[],[]).

您还在Erlang代码中使用整数设置Content-Length值。根据相同的文档,所有值都应该是字符串类型:

{"Content-Length", integer_to_list(string:len(Body))}

答案 1 :(得分:0)

您应该将其包含在Server中,例如

Server = "http://something.com/s.srf"

鉴于此,您可能想要更改变量名称。 (并且不要忘记包含协议!)。

来源:httpc docs