2

我正在制作一个将一些 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 文字。

提前致谢

韦尔奇

4

3 回答 3

4

您需要转义内容中的所有不安全字符,如下所示:

use URI::Escape;
$ret->content("xml_request=".uri_escape($XML));
于 2011-03-22T18:52:17.343 回答
3

您错误地构建了您的application/x-www-form-urlencoded文档。正确构造它的最简单方法是直接使用HTTP ::Request::CommonPOST

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-22T21:12:09.787 回答
-1

作为旁注,'+' 不需要 URL 编码,所以我不清楚你为什么在 XML 中对其进行编码。那一边

我认为如果你在它的构造函数中传递 HTTP::Request 一个预先格式化的字符串,它就不会触及数据。

my $ret = HTTP::Request->new('POST', $XMLurl, undef, "xml_request=".$XML); 
于 2011-03-22T18:57:39.257 回答