如何为Image::Grab提供UA属性?

How to provide UA attributes for Image::Grab?

我尝试在使用 Image::Grab 时添加一些 UA 属性,例如:

my $fp = 'sha256328...';
my $pic = Image::Grab->new( ua => { ssl_opts => { SSL_fingerprint => $fp } } );

我也尝试过:

$pic->ua->ssl_opts( { SSL_fingerprint => $fp } ); 

无论哪种方式,UA (LWP) 似乎都没有正确设置。如何实现SSL_fingerprint设置?我需要它,因为网站的证书有一些链缺陷,如果不指定指纹,我就无法安全地连接它。

要设置 SSL 选项,您需要将 2 个参数传递给 LWP::UserAgentssl_opts 方法:选项的名称及其值。因此,你应该这样做:

$pic->ua->ssl_opts( SSL_fingerprint => $fp ); 

在您的代码中,您将散列引用传递给 ssl_opts ({ SSL_fingerprint => $fp })。当单个参数被传递给 ssl_opts 时,这个参数应该是一个选项的名称,而函数 returns 与此名称关联的值。

您的代码 $pic->ua 将 return 在 Image::Grab 对象中使用的 UA 对象。所以调用方法应该做正确的事情。

话虽如此,the documentation for ssl_opts() 包含这些示例:

my @keys = $ua->ssl_opts;
my $val = $ua->ssl_opts( $key );
$ua->ssl_opts( $key => $value );

看来您需要从代码中删除匿名哈希构造函数:

$pic->ua->ssl_opts( SSL_fingerprint => $fp ); 

或者,您可以尝试创建自己的 UA 对象:

my $ua = Image::Grab::RequestAgent->new(...);

然后将其插入到 Image::Grab 对象中:

$pic->ua($ua);

Image::Grab::RequestAgent 是 LWP::UserAgent 的子类,因此其构造函数将接受所有相同的参数。