【问题标题】:HTTP::Request and literal %2BHTTP::Request 和文字 %2B
【发布时间】:2011-03-22 18:41:36
【问题描述】:

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

提前致谢

韦尔奇

【问题讨论】:

    标签: perl httprequest lwp


    【解决方案1】:

    您需要像这样转义内容中的所有不安全字符:

    use URI::Escape;
    $ret->content("xml_request=".uri_escape($XML));
    

    【讨论】:

    • 嗨,尤金,它有效,我只改变:这个 $ret->content(uri_escape("xml_request=$XML"));到 $ret->content(xml_request=uri_escape($XML));而且效果很好。
    • @Welcho,@ikegami 已更正。
    【解决方案2】:

    您错误地构建了您的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
    

    【讨论】:

    • @daxim,我不知道它是“适当的方式”。我使用它是因为它既方便又简单。它也比uri_escape 更不容易被滥用。
    【解决方案3】:

    附带说明,'+' 不需要 URL 编码,所以我不清楚您为什么要在 XML 中对其进行编码。那一边

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

    my $ret = HTTP::Request->new('POST', $XMLurl, undef, "xml_request=".$XML); 
    

    【讨论】:

    • 感谢 vicTROLLA,感谢您的宝贵时间。
    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多