2
我試圖使用HTTP::Request::Common
對URL進行發佈。不幸的是,它似乎沒有用於身份驗證,我不得不切換回普通ol'HTTP::Request
。如果我並不需要驗證,我可以做整個事情是這樣的:Perl:使用HTTP :: Request :: Common與身份驗證進行發佈
use HTTP::Request::Common;
my $browser = LWP::UserAgent->new;
$browser->request (
POST $url,
Content => [
Name => $name,
Address => $address,
],
);
不幸的是,因爲認證的,我不得不回落到HTTP::Request
:
use HTTP::Request;
my $browser = LWP::UserAgent->new;
my $request = HTTP::Request;
my $request->authentication_basic($user, $pass);
my $request->method("POST");
my $request->url($url);
# Create Content Byte String
my $uri = URI->new('http://'); #URL isn't needed, just the object
$uri->form_query(Name => $name, Address => $address);
my $content = $uri->query;
$request->content($content);
# Set the headers for the content
$request->header("Content-Type" => "application/x-www-form-urlencoded");
$request->header("Content-Length" => length($content));
# Now send your request through your browser
my $response = $browser->request($request);
你可以看到代碼涉及更多。我想切換回HTTP::Request::Common
,因爲代碼看起來會更容易維護,但我如何處理身份驗證?
謝謝。這很容易。 perldoc顯示您在創建「HTTP :: Request」對象的同時執行'LWP :: UserAgent->請求'。是的,一旦你指出它就絕對有意義。語法給我一個循環。這是一個非面向對象的調用語法,它確實是一個構造函數。謝謝。這使我的程序更容易閱讀。 –