那么LWP :: UserAgent可以自动检测字符集吗?

时间:2011-06-29 08:45:14

标签: perl encoding lwp

  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响应编码?

1 个答案:

答案 0 :(得分:2)

请参阅HTTP::Message中的decoded_content文档:

  

$ mess-> decoding_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.