1
  our $ua = LWP::UserAgent->new;
  my $response = $ua->get($url);
  if($response->is_success) {
    my $perl_hash_or_arrayref = decode_json(encode("UTF-8", $response->decoded_content));

上面的代码将响应转换为UTF-8编码,无需告诉它响应使用的是哪种编码。

我认为这在某些情况下可能会失败。

如何明确告知LWP::UserAgent响应编码?

4

1 回答 1

2

请参阅decoded_contentin的文档HTTP::Message

$mess->decoded_content(%options)

    Returns the content with any Content-Encoding undone and for
 textual content the raw content encoded to Perl's Unicode strings.
 If the Content-Encoding or charset of the message is unknown this
 method will fail by returning undef.

    The following options can be specified.

    charset

        This override the charset parameter for text content. The value
 none can used to suppress decoding of the charset.
于 2011-06-29T08:59:20.277 回答