7-2 ��������P���R�G�i���g

¦b¤W¸`¤¤¡A§Ú­Ì¤¶²Ð¤F LWP::Simple ªº¥Îªk©M½d¨Ò¡C¨Ï¥Î LWP::Simple ¨Ó§ì¨úºô­¶¬Û·í¤è«K¡A¦ý¬O¥¦¤]¦³¤@¨Ç¯ÊÂI¡G
  1. ¦b§ì¨úºô­¶¹Lµ{¤¤¡AµLªk¶Ç·|ª¬ºA½X¡]Status Code¡^¡C±`¨£ªºª¬ºA½X¦³¤U¦C´XºØ¡G

    ª¬ºA½X»¡©ú
    200OK¡A¤@¤Á¥¿±`
    404§ä¤£¨ì©Ò­n¨Dªººô­¶
    408µ¥«Ý¦øªA¾¹¦^ÂЮɶ¡¹L¤[
    500¦øªA¾¹¤º³¡µo¥Í¿ù»~

  2. Perl µ{¦¡¨Ã¤£·|¦bªíÀY¡]Header¡^¦V¦øªA¾¹ªí©ú¦Û¤vªº¨­¥÷¡C
  3. ¤£¤ä´©¥N²z¦øªA¾¹¡]Proxy Server¡^¡C
¦]¦¹¡A­Y­n¯à°÷¨ú±o­n¨Dºô­¶ªºª¬ºA½X¡A´N¥²¶·­n¨Ï¥Î¸û½ÆÂøªº¼Ò²Õ¡C¥H¤Uªº³o­Ó¨Ò¤l¡A¥i¥H¥ý§PÂ_¬O§_§ì¨úºô­¶¦¨¥\¡A­Y¦¨¥\¡A«h¦L¥Xºô­¶­ì©l½X¡F­Y¤£¦¨¥\¡A«h¦L¥X¿ù»~°T®§¡Cµ{¦¡½X¡]tstatus.pl¡^¦p¤U¡G

­ì©lÀÉ¡]tstatus.pl¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;

$ua = new LWP::UserAgent;				# ²£¥Í UserAgent ª«¥ó
$request = new HTTP::Request('GET', $ARGV[0]);	# ²£¥Í Request ª«¥ó
$response = $ua->request($request);			# ¶}©l§ì¨úºô­¶¡A¨Ã±Nµ²ªG¶Ç·| $response
if ($response->is_success) {			# ­Y§ì¨úºô­¶¦¨¥\¡A«h¦L¥X HTML ­ì©l½X
	print $response->content;
} else {						# ­Y§ì¨úºô­¶¤£¦¨¥\¡A«h¦L¥X¿ù»~°T®§
	print $response->error_as_HTML;
}

¤U­±³o­Ó½d¨Ò¡]tproxy.pl¡^¡A«h¨Ï¥Î¤F HTTP ¥N²z¦øªA¾¹¨Ó´î¤Öºô¸ôªº¬y¶q¡G

­ì©lÀÉ¡]tproxy.pl¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;

$ua = new LWP::UserAgent;				# ²£¥Í UserAgent ª«¥ó
$ua->proxy('http', 'http://proxy.nthu.edu.tw/proxy.pac');	# ³]©w HTTP ªº¥N²z¦øªA¾¹
$ua->no_proxy('nthu.edu.tw');			# ¦b²M¤jªººô­¶¡]nthu.edu.tw¡^¤£¨Ï¥Î¥N²z¦øªA¾¹
$request = new HTTP::Request('GET', $ARGV[0]);	# ²£¥Í Request ª«¥ó
$response = $ua->request($request);			# ¶}©l§ì¨úºô­¶¡A¨Ã±Nµ²ªG¶Ç·| $response
if ($response->is_success) {			# ­Y§ì¨úºô­¶¦¨¥\¡A«h¦L¥X HTML ­ì©l½X
	print $response->content;
} else {						# ­Y§ì¨úºô­¶¤£¦¨¥\¡A«h¦L¥X¿ù»~°T®§
	print $response->error_as_HTML;
}

¦æ¤å¦Ü¦¹¡AŪªÌÀ³¸Ó¥i¥H·P¨ü¨ì¥Î Perl ¨Ó§ì¨úºô­¶ªº«K§Q©Ê»P¼u©Ê¡Cµy·L­×§ï¤W­zªº½d¨Òµ{¦¡¡A´N¥i¥H²£¥Í³\¦h¤£¦PªºÀ³¥Î¡A¥H¤U¬O¤@¨Ç¦³¥Î¡]©Î¦³½ì¡^ªºÀ³¥Î½d¨Ò¡G


Perl