我需要将RFC 1867上传到服务器上的wav文件的原始数据(没有riff头)。我写了一个perl脚本来做这件事。看起来像这样:
my $wav = new Audio::Wav;
my $read = $wav->read($wavtoupload);
my $rawdata = $read->read_raw($read->length()); #raw data w/o riff
my $url = "http://thehost.com";
my $req = HTTP::Request->new();
$req = POST $url,
Referer => 'hxxp://www.mydomain.com/some.php3',
Accept_Language => 'en-us',
Content_Type => 'form-data',
Accept_Encoding => 'wav',
User_Agent => 'Mozilla/4.0',
Host => 'www.mydomain.com',
Connection => 'Keep-Alive',
Content => [ $rawdata ];
print $req->as_string;
my $ua = LWP::UserAgent->new;
my $resp = $ua->request($req);
if ($resp->is_success) {
#...
}
根据http://search.cpan.org/~ether/HTTP-Message-6.11/lib/HTTP/Request/Common.pm:
“POST方法还支持用于基于表单的文件上载的 multipart / form-data 内容,如RFC 1867中所指定。您可以通过指定'form-的内容类型来触发此内容格式数据'作为请求标题之一。“
但是,服务器正在接收Content_Type =“form-data”并不喜欢它,因为它需要“multipart / form-data”。 Content_Length也是0,并且正在上传正文。
我做错了什么?感谢
答案 0 :(得分:1)
你没有发布:
的输出print $req->as_string;
您做错的一件事是您没有将数据组合成键/值对,这就是所有HTTP请求组织数据的方式。
Content_Length也是0
那是因为所有数据都被用作键/值对的关键部分,因此没有值,并且没有值的Content-Length为0.
然而,服务器不应该收到标题:
Content_Type = "form-data"
对于原始请求,我得到:
POST http://thehost.com
Connection: Keep-Alive
Accept-Encoding: wav
Accept-Language: en-us
Host: www.mydomain.com
Referer: hxxp://www.mydomain.com/some.php3
User-Agent: Mozilla/4.0
Content-Length: 82
Content-Type: multipart/form-data; boundary=xYzZY
--xYzZY
Content-Disposition: form-data; name="mydata"
hello world
--xYzZY--
您可以清楚地看到,Content-Type标头为multipart/form-data
。那是因为:
您可以通过指定内容类型来触发此内容格式 '形状数据'作为请求标题之一。
换句话说,perl模块在实际请求中创建标题Content-Type: multipart/form-data
。
这是我使用的代码:
use strict;
use warnings;
use 5.020;
use Data::Dumper;
use HTTP::Message;
use HTTP::Request::Common;
my $rawdata = "hello world";
my $url = "http://thehost.com";
my $requ = POST $url,
Referer => 'hxxp://www.mydomain.com/some.php3',
Accept_Language => 'en-us',
Content_Type => 'form-data',
Accept_Encoding => 'wav',
User_Agent => 'Mozilla/4.0',
Host => 'www.mydomain.com',
Connection => 'Keep-Alive',
Content => [ mydata => $rawdata ];
say $requ->as_string;
以下显示指定键/值对v。仅指定值:
之间的区别use strict;
use warnings;
use 5.020;
use Data::Dumper;
use HTTP::Message;
use HTTP::Request::Common;
my $rawdata = "hello world";
my $url = "http://thehost.com";
my $requ = POST $url,
Referer => 'hxxp://www.mydomain.com/some.php3',
Accept_Language => 'en-us',
Content_Type => 'form-data',
Accept_Encoding => 'wav',
User_Agent => 'Mozilla/4.0',
Host => 'www.mydomain.com',
Connection => 'Keep-Alive',
Content => [
mykey => 'myValue',
$rawdata,
];
say $requ->as_string;
--output:--
POST http://thehost.com
Connection: Keep-Alive
Accept-Encoding: wav
Accept-Language: en-us
Host: www.mydomain.com
Referer: hxxp://www.mydomain.com/some.php3
User-Agent: Mozilla/4.0
Content-Length: 142
Content-Type: multipart/form-data; boundary=xYzZY
--xYzZY
Content-Disposition: form-data; name="mykey" #<===HERE
myValue #<======AND HERE
--xYzZY
Content-Disposition: form-data; name="hello world" #<===COMPARED TO
#<======COMPARED TO
--xYzZY--
裸值最终被用作键,因为没有值,Content-Length最终为0。
根据docs,以下是指定文件上传的方式:
use strict;
use warnings;
use 5.020;
use Data::Dumper;
use HTTP::Message;
use HTTP::Request::Common;
my $rawdata = "hello world";
my $url = "http://thehost.com";
my $requ = POST $url,
Referer => 'hxxp://www.mydomain.com/some.php3',
Accept_Language => 'en-us',
Content_Type => 'form-data',
Accept_Encoding => 'wav',
User_Agent => 'Mozilla/4.0',
Host => 'www.mydomain.com',
Connection => 'Keep-Alive',
Content => [
mykey => 'myValue', #some other key/value pair
cool_sounds => [undef, #file upload key/value pair
'myfile.wav',
Content => $rawdata
]
];
say $requ->as_string;
--output:--
POST http://thehost.com
Connection: Keep-Alive
Accept-Encoding: wav
Accept-Language: en-us
Host: www.mydomain.com
Referer: hxxp://www.mydomain.com/some.php3
User-Agent: Mozilla/4.0
Content-Length: 176
Content-Type: multipart/form-data; boundary=xYzZY
--xYzZY
Content-Disposition: form-data; name="mykey"
myValue
--xYzZY
Content-Disposition: form-data; name="cool_sounds"; filename="myfile.wav"
hello world
--xYzZY--
如果您需要为文件部分设置Content-Type,可以这样做:
use strict;
use warnings;
use 5.020;
use Data::Dumper;
use HTTP::Message;
use HTTP::Request::Common;
my $rawdata = "hello world";
my $url = "http://thehost.com";
my $requ = POST $url,
Referer => 'hxxp://www.mydomain.com/some.php3',
Accept_Language => 'en-us',
Content_Type => 'form-data',
Accept_Encoding => 'wav',
User_Agent => 'Mozilla/4.0',
Host => 'www.mydomain.com',
Connection => 'Keep-Alive',
Content => [
mykey => 'myValue', #some other key/value pair
cool_sounds => [undef, #file upload key/value pair
'myfile.wav',
'Content-Type' => 'audio/x-wav',
Content => $rawdata
] #=>If one of the values in the $form_ref is an array reference...
];
say $requ->as_string;
--output:--
OST http://thehost.com
Connection: Keep-Alive
Accept-Encoding: wav
Accept-Language: en-us
Host: www.mydomain.com
Referer: hxxp://www.mydomain.com/some.php3
User-Agent: Mozilla/4.0
Content-Length: 203
Content-Type: multipart/form-data; boundary=xYzZY
--xYzZY
Content-Disposition: form-data; name="mykey"
myValue
--xYzZY
Content-Disposition: form-data; name="cool_sounds"; filename="myfile.wav"
Content-Type: audio/x-wav
hello world
--xYzZY--
并且,如果您需要将文件部分的Content-Disposition设置为其他内容:
use strict;
use warnings;
use 5.020;
use Data::Dumper;
use HTTP::Message;
use HTTP::Request::Common;
my $rawdata = "hello world";
my $url = "http://thehost.com";
my $requ = POST $url,
Referer => 'hxxp://www.mydomain.com/some.php3',
Accept_Language => 'en-us',
Content_Type => 'form-data',
Accept_Encoding => 'wav',
User_Agent => 'Mozilla/4.0',
Host => 'www.mydomain.com',
Connection => 'Keep-Alive',
Content => [
mykey => 'myValue', #some other key/value pair
cool_sounds => [undef, #file upload key/value pair
'myfile.wav',
'Content-Type' => 'audio/x-wav',
'Content-Disposition' => 'file',
Content => $rawdata
]
];
say $requ->as_string;
--output:--
POST http://thehost.com
Connection: Keep-Alive
Accept-Encoding: wav
Accept-Language: en-us
Host: www.mydomain.com
Referer: hxxp://www.mydomain.com/some.php3
User-Agent: Mozilla/4.0
Content-Length: 155
Content-Type: multipart/form-data; boundary=xYzZY
--xYzZY
Content-Disposition: form-data; name="mykey"
myValue
--xYzZY
Content-Disposition: file
Content-Type: audio/x-wav
hello world
--xYzZY--
请注意,在这种情况下,文件部分没有名称属性。
顺便说一下,是什么让你认为不发布你的use statements
是有帮助的?别这么做。