OZの部屋
なんでも日記です
 



サーバー側でディレクトリに対してBasic認証をかけている場合の
LWPの使い方
IEで対象ページにアクセスしたとき出てくる認証画面の表示名がrealmになる。
以下の$upfile,$target,$user,$pass,$realmを設定して
perl upload.pl
と実行する。

# upload.pl Basic認証付のページにファイルをアップロードする
use HTTP::Request::Common;
use LWP::UserAgent;

#アップロードするファイル
$upfile='abc.txt';
#ファイル名入力欄のオブジェクト名
$fileform='file1';
#アップロードページのサブミットactionページ
$target='http://www.xxx.jp/cgi-bin/upload.cgi';
#Basic認証ユーザー名
$user='user1';
#パスワード
$pass='xyzxyz';
#Basic認証の領域名
$realm='Input your password';
####### ここまで変数をしてする ########
# これ以降は変更不要 ###

$target=~m#http(s?)://([^/]+)/.*#;
$site="$2:".($1 eq 's'?443:80);

$ua = LWP::UserAgent->new;
$ua->credentials( $site, $realm, $user => $pass);

$res=$ua->request(POST $target,
Content_Type => 'form-data',
Content => [ $fileform => [$upfile],
]);
print $res->as_string;



HTTP Analyzer で実行を見てみると
送信Streamは
POST /cgi-bin/upload.cgi HTTP/1.1
TE: deflate,gzip;q=0.3
Connection: TE, close
Host: www.xxxx.jp
User-Agent: libwww-perl/5.805
Content-Length: 173
Content-Type: multipart/form-data; boundary=xYzZY

--xYzZY
Content-Disposition: form-data; name="file1"; filename="abc.txt"
Content-Type: text/plain

123
111
44
--xYzZY--


Responsは
HTTP/1.1 401 Authorization Required
Date: Mon, 30 Oct 2006 15:01:30 GMT
Server: Apache/1.3.33 (Unix) mod_ssl/2.8.23 OpenSSL/0.9.7a
WWW-Authenticate: Basic realm="Input your password"
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1

1d9
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>401 Authorization Required</TITLE>
</HEAD><BODY>
<H1>Authorization Required</H1>
This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.<P>
<HR>
<ADDRESS>Apache/1.3.33 Server at ns.xxxx.jp Port 80</ADDRESS>
</BODY></HTML>


認証後の動作までHTTP Analyzerが追えていないようですが、
実際にちゃんとアップロードされていました。


コメント ( 0 ) | Trackback ( 0 )
Messenger この記事をはてなブックマークに追加 mixiチェック シェア


« wgetでpost ログ... perl LWP アップ... »
 
コメント
 
コメントはありません。
コメントを投稿する
 
名前
タイトル
URL
コメント
規約に同意の上 コメント投稿を行ってください。
※文字化け等の原因になりますので、顔文字の利用はお控えください。
下記数字4桁を入力し、投稿ボタンを押してください。この数字を読み取っていただくことで自動化されたプログラムによる投稿でないことを確認させていただいております。
数字4桁
 
この記事のトラックバック Ping-URL