1

我想从 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)

-> 我试过这个 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”放在哪里,有什么想法吗?

4

2 回答 2

0

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

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

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

{"Content-Length", integer_to_list(string:len(Body))}
于 2012-07-14T17:46:43.590 回答
0

你应该把它包括在Server,例如

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

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

来源:httpc 文档

于 2012-07-14T17:47:11.093 回答