xml请求
XmlList = xmlencode(ParamList),
ContentType = "application/xml",
HTTPOptions = [{timeout, 3000}],
Options = [],
httpc:request(post, {"www.baidu.com", [], ContentType, XmlList}, HTTPOptions, Options) of
{error, Reason} ->
?ERROR("---{error,Reason}~p~n ", [Reason]),
exit(Reason);
{ok, {{_, 200, _}, _HeadList, HtmlResultXml}} ->
CodeStr = xml:string("return_code", HtmlResultXml),
if
CodeStr == "SUCCESS" orelse CodeStr == 'SUCCESS' ->
ResultCode = xml:string("result_code", HtmlResultXml),
if
ResultCode == "SUCCESS" orelse ResultCode == 'SUCCESS' ->
PrepayId = xml:string("prepay_id", HtmlResultXml),
PrepayId;
true ->
ErrCodeMsg = xml:string("err_code_des", HtmlResultXml),
?ERROR("web请求数据>>ResultCode:~p ~ts~n ", [ResultCode, ErrCodeMsg]),
exit(null_object)
end;
true ->
ErrReturnMsg = xml:string("return_msg", HtmlResultXml),
?ERROR("web请求数据return_msg>>~p ~ts~n ", [CodeStr, ErrReturnMsg]),
exit(return_code_fail)
end;
R ->
?ERROR("====other~p~n", [R]),
exit(error_http)
end.
%% @fun 转成xml
xmlencode(List) ->
ParamList = encode1(List, []),
XmlList = xmerl:export_simple_content(ParamList, xmerl_xml),
"<xml>" ++ lists:flatten(XmlList) ++ "</xml>".
encode1([], L) ->
L;
encode1([{Key, Value} | List], L) ->
NewValue =
case is_list(Value) of
true ->
case hd(Value) of
{_, _} ->
encode1(Value, []);
_ ->
case is_change_list(Value) of
false ->
io_lib:format("~p", [Value]);
_ ->
[Value]
end
end;
_ ->
[util:to_list(Value)]
end,
encode1(List, [{util:to_atom(Key), NewValue} | L]).
is_change_list([]) ->
true;
is_change_list([S | Str]) ->
case is_integer(S) of
true ->
is_change_list(Str);
_ ->
false
end.
json 请求
JsonList = jsone:encode(ParamList),
ContentType = "application/json",
HTTPOptions = [{timeout, 3000}],
Options = [],
Url = "www.baidu.com",
case httpc:request(post, {Url, [], ContentType, JsonList}, HTTPOptions, Options) of
{error, Reason} ->
?ERROR("---{error,Reason}~p~n ", [Reason]),
exit(Reason);
{ok, {{_, 200, _}, _HeadList, HtmlResultJson}} ->
ResultJson = jsone:decode(util:to_binary(HtmlResultJson)),
Code = maps:get(<<"code">>, ResultJson),
if
Code == 200 orelse Code == <<"200">> ->
Data = maps:get(<<"data">>, ResultJson),
OrderNo = maps:get(<<"orderNo">>, Data),
OrderNo;
true ->
Msg = maps:get(<<"msg">>, ResultJson),
?ERROR("web请求数据:~p, msg:~ts~n", [Code, Msg]),
exit(?ERROR_FAIL)
end;
R ->
?ERROR("====other~p~n", [R]),
exit(error_http)
end.
json内容转换样式:
case httpc:request(post,
{"www.baidu.com",
[],"application/json", "{ \"user\":\"yamma3\",\"pwd\":\"45b9ea1d748393c9bc2ed39d10ca665d\",\"net\":\"0\",\"token\":\"token\",\"version\":\"1\"}"},[],[]) of
{ok, {_,_,Body}}-> Body;
{error, Reason}->io:format("error cause ~p~n",[Reason])
end.
text请求
Type = "application/x-www-form-urlencoded",
HTTPOptions = [{timeout, 6000}],
Options = [],
Url = "www.baidu.com",
case httpc:request(post, {Url, [], Type, ParamStr}, HTTPOptions, Options) of
{error, Reason} ->
?ERROR("---{error,Reason}~p~n ", [Reason]),
exit(Reason);
{ok, {_, _, Result}} ->
Response = jsone:decode(util:to_binary(Result)),
Code = maps:get(<<"code">>, Response),
if
Code == 0 ->
ok;
true ->
Msg = maps:get(<<"errmsg">>, Response),
?ERROR("web请求数据:~p, msg:~ts~n", [Code, Msg]),
noop
end;
ChargeMoXieR ->
?ERROR("====other~p~n", [R]),
noop
end.
%% 列表转换 % JoinTuple key JoinSon Value 例如: "&key=value"
change_list_url(List) ->
change_list(List, "&", "=").
change_list([], _JoinTuple, _JoinSon) ->
[];
change_list(List, JoinTuple, JoinSon) ->
ListSort = lists:reverse(List),
ListConCat = change_list(ListSort, [], JoinTuple, JoinSon),
lists:concat(ListConCat).
change_list([], L, _JoinTuple, _JoinSon) ->
L;
change_list([{Key, Value} | List], L, JoinTuple, JoinSon) ->
Result =
if
List == [] ->
[Key, JoinSon, Value];
true ->
[JoinTuple, Key, JoinSon, Value]
end,
change_list(List, Result ++ L, JoinTuple, JoinSon);
change_list([Value | List], L, JoinTuple, JoinSon) ->
Result =
if
List == [] ->
[Value];
true ->
[JoinTuple, Value]
end,
change_list(List, Result ++ L, JoinTuple, JoinSon).