我正在尝试将URL作为查询字符串传递,以便可以被其他网站读取然后使用:
www.example.com/domain?return_url=/another/domain
Run Code Online (Sandbox Code Playgroud)
获取返回为:
www.example.com/domain?return_url=%2Fanother%2Fdomain
Run Code Online (Sandbox Code Playgroud)
是否有一种方法可以通过其他应用程序使用转义字符读取和解析此URL?
我能想到的唯一方法是以某种方式对其进行编码,因此它就像这样:
www.example.com/domain?return_url=L2Fub3RoZXIvZG9tYWlu
那么其他应用程序可以解码和使用?
www.example.com/domain?return_url=%2Fanother%2Fdomain
这称为URL编码.不是因为你在其中放了一个URL,而是因为它编码了URL中具有特殊含义的字符.
该%2F对应于斜线/.你可能也看过%20前面的,这是一个空间.
Putting a complete URI into a URL parameter of another URI is totally fine.
http://example.org?url=http%3A%2F%2Fexample.org%2Ffoo%3Fbar%3Dbaz
Run Code Online (Sandbox Code Playgroud)
The application behind the URL you are calling needs to be able to understand URL encoding, but that is a trivial thing. Typical web frameworks and interfaces to the web (like CGI.pm or Plack in Perl) will do that. You should not have to care about it a all.
To URL-encode something in Perl, you have several options.
You could use the URI module to create the whole URI including the URL encoded query.
use URI;
my $u = URI->new("http://example.org");
$u->query_form( return_url => "http://example.org/foo/bar?baz=qrr");
print $u;
__END__
http://example.org?return_url=http%3A%2F%2Fexample.org%2Ffoo%2Fbar%3Fbaz%3Dqrr
Run Code Online (Sandbox Code Playgroud)
This seems like the natural thing to do.
You could also use the URI::Encode module, which gives you a uri_encode功能.如果要在不构建URI对象的情况下编码字符串,这非常有用.
Run Code Online (Sandbox Code Playgroud)use URI::Encode qw(uri_encode uri_decode); my $encoded = uri_encode($data); my $decoded = uri_decode($encoded);
所有这些都是网络运作的正常部分.无需执行Base 64编码.
| 归档时间: |
|
| 查看次数: |
482 次 |
| 最近记录: |