我正在制作一个脚本,将一些XML发送到另一个服务器,但我在使用加号(+)时遇到了问题。下面是我的代码:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
my $XML = qq|
<?xml version="1.0" encoding="UTF-8"?>
<ServiceAddRQ>
<Service code="Ws%2BsuHG7Xqk01RaIxm2L/w1L">
<ContractList>
<Contract>
<Name>CGW-TODOSB2B</Name>
</Contract>
</ContractList>
</Service>
</ServiceAddRQ>
|;
utf8::encode($XML);
my $ua = LWP::UserAgent->new;
$ua->timeout(120);
my $ret = HTTP::Request->new('POST', $XMLurl);
$ret->content_type('application/x-www-form-urlencoded');
$ret->content("xml_request=$XML");
my $response = $ua->request($ret);正如您在属性代码中看到的,值字符串具有%2B,而另一台服务器接收到值"Ws+suHG7Xqk01RaIxm2L/w1L“。
如何发送%2B文本。
提前感谢
韦尔奇
发布于 2011-03-23 02:52:17
您需要转义内容中的所有不安全字符,如下所示:
use URI::Escape;
$ret->content("xml_request=".uri_escape($XML));发布于 2011-03-23 05:12:10
您错误地构造了application/x-www-form-urlencoded文档。正确构建它的最简单方法是直接使用HTTP::Request::Common的POST
use HTTP::Request::Common qw( POST );
my $request = POST($XMLurl, [ xml_request => $XML ]);
my $response = $ua->request($request);或间接地
my $response = $ua->post($XMLurl, [ xml_request => $XML ]);请求的主体将是
Ws%252BsuHG7Xqk01RaIxm2L/w1L而不是
Ws%2BsuHG7Xqk01RaIxm2L/w1L所以你最终会得到
Ws%2BsuHG7Xqk01RaIxm2L/w1L而不是
Ws+suHG7Xqk01RaIxm2L/w1L发布于 2011-03-23 02:57:39
顺便说一下,'+‘不需要URL编码,所以我不清楚为什么要在XML中编码它。抛开这个不谈
我认为如果你在构造函数中传递HTTP::Request一个预格式化的字符串,它将不会接触到数据。
my $ret = HTTP::Request->new('POST', $XMLurl, undef, "xml_request=".$XML); https://stackoverflow.com/questions/5396216
复制相似问题