使用LWP::UserAgent提交HTTP POST请求,将XML文件内容作为正文 #/usr/bin/perl-w 严格使用; 使用文件句柄; 使用LWP::UserAgent; 使用HTTP::请求; 子打印文件($){ 我的$fileHandle=$\u0]; 而(){ 我的$line=$\ux; chomp($line); 打印“$line\n”; } } my$message=新文件句柄; 打开$message,“
我不明白使用LWP::UserAgent提交HTTP POST请求,将XML文件内容作为正文 #/usr/bin/perl-w 严格使用; 使用文件句柄; 使用LWP::UserAgent; 使用HTTP::请求; 子打印文件($){ 我的$fileHandle=$\u0]; 而(){ 我的$line=$\ux; chomp($line); 打印“$line\n”; } } my$message=新文件句柄; 打开$message,“,xml,perl,Xml,Perl,我不明白printFile的作用是什么,但您将文件句柄$message作为消息正文传递,而不是文件的内容 请注意以下几点 始终使用警告而不是-w注释行选项 不要使用子程序原型子打印文件($)应该只是子打印文件 无需使用FileHandle来处理文件 文件打开失败的原因在$中。您应该始终将其包含在die字符串中 切勿使用间接对象表示法新建LWP::UserAgent应为LWP::UserAgent->新建 这个版本的代码可能会更好一些,但我没有办法测试它 #!/usr/bin/perl -w
printFile
的作用是什么,但您将文件句柄$message
作为消息正文传递,而不是文件的内容
请注意以下几点
- 始终
而不是使用警告
注释行选项-w
- 不要使用子程序原型<代码>子打印文件($)应该只是
子打印文件
- 无需
来处理文件使用FileHandle
- 文件
打开失败的原因在
$中代码>。您应该始终将其包含在
字符串中die
- 切勿使用间接对象表示法<代码>新建LWP::UserAgent应为
LWP::UserAgent->新建
#!/usr/bin/perl -w
use strict;
use FileHandle;
use LWP::UserAgent;
use HTTP::Request;
sub printFile($) {
my $fileHandle = $_[0];
while (<$fileHandle>) {
my $line = $_;
chomp($line);
print "$line\n";
}
}
my $message = new FileHandle;
open $message, '<', 'Request.xml' or die "Could not open file\n";
printFile($message);
my $url = qq{https://host:8444};
my $ua = new LWP::UserAgent(ssl_opts => { verify_hostname => 0 });
$ua->proxy('http', 'proxy:8080');
$ua->no_proxy('localhost');
my $req = new HTTP::Request(POST => $url);
$req->header('Host' => "host:8444");
$req->content_type("application/xml; charset=utf-8");
$req->content($message);
$req->authorization_basic('TransportUser', 'TransportUser');
my $response = $ua->request($req);
my $content = $response->decoded_content();
print $content;
#/usr/bin/perl
严格使用;
使用警告;
使用LWP;
我的$message=do{
打开我的$fh,'请发布程序的文本,而不是图像,以便我们可以复制/粘贴它。您以前在“现在确定”中已被告知这一点,请检查。如果我传递内容,则不传递文件。但我想传递xml文件
#!/usr/bin/perl
use strict;
use warnings;
use LWP;
my $message = do {
open my $fh, '<', 'Request.xml' or die "Could not open file: $!";
local $/;
<$fh>;
};
my $url = 'https://host:8444';
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
$ua->proxy(qw/ http proxy:8080 /);
$ua->no_proxy('localhost');
my $req = HTTP::Request->new(POST => $url);
$req->header(Host => 'host:8444');
$req->content_type('application/xml; charset=utf-8');
$req->content($message);
$req->authorization_basic('TransportUser', 'TransportUser');
my $response = $ua->request($req);
my $content = $response->decoded_content;
print $content;