1

我使用 erlang,我使用与我的电脑相关的调制解调器

我想从调制解调器向我的电话号码发送消息

我开发了这个功能,它工作得很好

send(To) ->
Message =http_urii:encode("Vous n'avez pas d'écheance à payer."),
    Url = io_lib:format("http://192.168.1.5:9998/sendsms?tel=~s&text=~s", [To, Message]),
    http:request(lists:flatten(Url), sms).

我的问题与带有变音符号的符号有关,例如 é , à

因为在我的手机中我收到了短信但特殊字符没有以正确的格式显示é变成了另一个字符,à变成了另一个字符

我想在我的手机中收到这条信息:Vous n'avez pas d'écheance à payer。

我修改了发送的源代码

 send(To) ->
    Message =http_urii:encode("Vous n'avez pas d'%E9cheance %EA payer."),
        Url = io_lib:format("http://192.168.1.5:9998/sendsms?tel=~s&text=~s", [To, Message]),
        http:request(lists:flatten(Url), sms).

但在我的手机中,我收到了这条消息Vous n'avez pas d'%E9cheance %EA payer。不编码特殊字符

http_urii.erl的源代码是:

-module(http_urii).

-export([parse/1, encode/1, decode/1]).

%%%=========================================================================
%%%  API
%%%=========================================================================
parse(AbsURI) ->
    case parse_scheme(AbsURI) of
    {error, Reason} ->
        {error, Reason};
    {Scheme, Rest} ->
        case (catch parse_uri_rest(Scheme, Rest)) of
        {UserInfo, Host, Port, Path, Query} ->
            {Scheme, UserInfo, Host, Port, Path, Query};
        _  ->
            {error, {malformed_url, AbsURI}}
        end
    end.

encode(URI) ->
    Reserved = sets:from_list([$;, $:, $@, $&, $=, $+, $,, $/, $?,
                   $#, $[, $], $<, $>, $\", ${, $}, $|,
                   $\\, $', $^, $%, $ ]),
    lists:append(lists:map(fun(Char) ->
                   uri_encode(Char, Reserved)
               end, URI)).

decode([$%,Hex1,Hex2|Rest]) ->
    [hex2dec(Hex1)*16+hex2dec(Hex2)|decode(Rest)];
decode([First|Rest]) ->
    [First|decode(Rest)];
decode([]) ->
    [].

%%%========================================================================
%%% Internal functions
%%%========================================================================
parse_scheme(AbsURI) ->
    case split_uri(AbsURI, ":", {error, no_scheme}, 1, 1) of
    {error, no_scheme} ->
        {error, no_scheme};
    {StrScheme, Rest} ->
        case list_to_atom(http_util:to_lower(StrScheme)) of
        Scheme when Scheme == http; Scheme == https ->
            {Scheme, Rest};
        Scheme ->
            {error, {not_supported_scheme, Scheme}}
        end
    end.

parse_uri_rest(Scheme, "//" ++ URIPart) ->

    {Authority, PathQuery} =
    case split_uri(URIPart, "/", URIPart, 1, 0) of
        Split = {_, _} ->
        Split;
        URIPart ->
        case split_uri(URIPart, "\\?", URIPart, 1, 0) of
            Split = {_, _} ->
            Split;
            URIPart ->
            {URIPart,""}
        end
    end,

    {UserInfo, HostPort} = split_uri(Authority, "@", {"", Authority}, 1, 1),
    {Host, Port} = parse_host_port(Scheme, HostPort),
    {Path, Query} = parse_path_query(PathQuery),
    {UserInfo, Host, Port, Path, Query}.


parse_path_query(PathQuery) ->
    {Path, Query} =  split_uri(PathQuery, "\\?", {PathQuery, ""}, 1, 0),
    {path(Path), Query}.

parse_host_port(Scheme,"[" ++ HostPort) -> %ipv6
    DefaultPort = default_port(Scheme),
    {Host, ColonPort} = split_uri(HostPort, "\\]", {HostPort, ""}, 1, 1),
    {_, Port} = split_uri(ColonPort, ":", {"", DefaultPort}, 0, 1),
    {Host, int_port(Port)};

parse_host_port(Scheme, HostPort) ->
    DefaultPort = default_port(Scheme),
    {Host, Port} = split_uri(HostPort, ":", {HostPort, DefaultPort}, 1, 1),
    {Host, int_port(Port)}.

split_uri(UriPart, SplitChar, NoMatchResult, SkipLeft, SkipRight) ->
    case inets_regexp:first_match(UriPart, SplitChar) of
    {match, Match, _} ->
        {string:substr(UriPart, 1, Match - SkipLeft),
         string:substr(UriPart, Match + SkipRight, length(UriPart))};
    nomatch ->
        NoMatchResult
    end.

default_port(http) ->
    80;
default_port(https) ->
    443.

int_port(Port) when is_integer(Port) ->
    Port;
int_port(Port) when is_list(Port) ->
    list_to_integer(Port).

path("") ->
    "/";
path(Path) ->
    Path.

uri_encode(Char, Reserved) ->
    case sets:is_element(Char, Reserved) of
    true ->
        [ $% | http_util:integer_to_hexlist(Char)];
    false ->
        [Char]
    end.

hex2dec(X) when (X>=$0) andalso (X=<$9) -> X-$0;
hex2dec(X) when (X>=$A) andalso (X=<$F) -> X-$A+10;
hex2dec(X) when (X>=$a) andalso (X=<$f) -> X-$a+10.

现在我将 http_urii 更改为标准的 http_uri

但是当我执行我的项目时

我有这个错误: Erlang R13B03 (erts-5.7.4) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4  (abort with ^G)
1> exception: [{http_uri,encode,
                      ["Vous n'avez pas d'écheance à payer."]},
            {sms_alert,send,1},
            {webrpc_api,build_response,2},
            {webrpc_api,format_response,2},
            {webrpc_api,process_command,2},
            {httpd_response,traverse_modules,2},
            {httpd_response,generate_and_send_response,1},
            {httpd_request_handler,handle_response,1}]

我知道我使用的是 erlang 版本 13,但是我在项目的 src 中制作了模块 http_uri.erl ,所以通常应该可以使用

4

1 回答 1

0

如果您查看http_urii:encode/1返回的内容,您会发现它不是您明确使用的内容。在我的机器上,我得到:

4> http_urii:encode("Vous n'avez, pas d'énheance à payer.").
"Vous%20n%27avez%2C%20pas%20d%27énheance%20à%20payer."

标准 Erlang 版本中有一个模块http_uri在这种情况下返回与您的http_urii.

于 2013-01-10T15:11:04.437 回答