24-7 ��������P�B�z

­Y­n¨Ï¥Î WSH ¨Óª½±µ§ì¨úºô­¶¡A¥i¥H¨Ï¥Î Visual Basic 6 ªº¤¸¥ó InetCtls.Inet¡A¦pªG§Aªº¾÷¾¹©|¥¼¦w¸Ë¦¹¤¸¥ó¡A¥i¥H¨Ì·Ó¤U¦C¤è¦¡¨Ó¶i¦æ¦w¸Ë¡G
  1. ±q¤U¦Cºô§}¤U¸üÀ£ÁYÀÉ®× msinet.cab¡G
    http://activex.microsoft.com/controls/vb6/msinet.cab
    ¡]ªñºÝ³Æ¥÷¡G...\wsh\download\msinet.cab¡^
  2. ¹ï msinet.cab ¶i¦æ¸ÑÀ£ÁY¡A±o¨ì MSINET.INF ©M MSINET.OCX¡A±N³o¨â­ÓÀɮשñ¨ì c:\windows\system32 ¥Ø¿ý¤U¡C
  3. ¶}±Ò DOS µøµ¡¡A¶i¤J c:\windows\system32 ¥Ø¿ý¡A°õ¦æ¡uregsvr32 msinet.ocx¡v¡A§Y¥i§¹¦¨¦w¸Ë¡C
¦w¸Ë§¹¦¨«á¡A§Ú­Ì´N¥i¥H¨Ï¥Î WSH ¨Óª½±µ§ì¨úºô­¶¡A½Ð¨£¤U¦C½d¨Ò¡G

­ì©lÀÉ¡]getWebPage01.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// §ì¨ú¤@­Óºô­¶
inet=new ActiveXObject("InetCtls.Inet");		// ¨ú±o Inet Control ª«¥ó
inet.Url="http://www.cs.nthu.edu.tw";			// ±ý¤U¸ü¤§ºô­¶
inet.RequestTimeOut=60;					// ³]©w¹Á¸Õ®É¶¡
WScript.Echo("Downloading \""+inet.Url+"\"...");
content = inet.OpenURL();				// ¤U¸üºô­¶
WScript.Echo(content);					// Åã¥Üºô­¶¤º®e

¨Ï¥Î³q¥Î¹Bºâ¦¡¡A§Ú­Ì¥i¥H¦b§ì¨úºô­¶«á¡AÅã¥Ü¦¹ºô­¶ªº¼ÐÃD¡A¨Ò¦p¡G

­ì©lÀÉ¡]getWebPage02.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// §ì¨ú¤@­Óºô­¶¡A¨Ã§ì¥X¨ä¼ÐÃD
inet=new ActiveXObject("InetCtls.Inet");		// ¨ú±o Inet Control ª«¥ó
inet.Url="http://www.cs.nthu.edu.tw";			// ±ý¤U¸ü¤§ºô­¶
inet.RequestTimeOut=60;					// ³]©w¹Á¸Õ®É¶¡
WScript.Echo("Downloading \""+inet.Url+"\"...");
content = inet.OpenURL();				// ¤U¸üºô­¶
pattern = /<title>(.*)<\/title>/i;			// ©w¸q³q¥Îªí¥Ü¦¡
title = pattern.exec(content);				// §ì¥X¼ÐÃD
WScript.Echo("¦ì©ó¡u"+inet.Url+"¡vªººô­¶ªº¼ÐÃD¬O¡u"+RegExp.$1+"¡v¡I");	// Åã¥Üµ²ªG

§Ú­Ì¤]¥i¥H¦b§ì¨ú¤@­Óºô­¶«á¡A¥ß§Y±Nºô­¶Àx¦s¨ìµwºÐ¤¤ªº¬Y­ÓÀɮסG

­ì©lÀÉ¡]getWebPage03.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// §ì¨ú¤@­Óºô­¶¡A¨Ã±N¨ä¤º®e¦s¤J¤@­ÓÀÉ®×
inet=new ActiveXObject("InetCtls.Inet");		// ¨ú±o Inet Control ª«¥ó
inet.Url="http://www.cs.nthu.edu.tw";			// ±ý¤U¸ü¤§ºô­¶
inet.RequestTimeOut=20;					// ³]©w¹Á¸Õ®É¶¡
WScript.Echo("Downloading \""+inet.Url+"\"...");
content = inet.OpenURL();				// ¤U¸üºô­¶
// ¥H¤U±Nºô­¶¤º®e¼g¤J¥»¾÷ÀÉ®×
fso = new ActiveXObject("Scripting.FileSystemObject");
forReading=1, forWriting=2;
fileName="test.htm";
fid=fso.OpenTextFile(fileName, forWriting, true);
fid.Write(content);
fid.Close();
WScript.Echo("±q¡u"+inet.Url+"¡v§ì¨ìªº¤º®e¤w³Q¦s¤J¡u"+fileName+"¡v¡I");

§Ú­Ì¤]¥i¥H¦b§ì¨úºô­¶«á¡A§Q¥Î³q¥Îªí¥Üªk¨Ó§ì¥Xºô­¶¤¤ªº³sµ²ºô§}©M¬ÛÃö¤å¦r¡]Ancher Texts¡^¡G

­ì©lÀÉ¡]getWebPage04.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// §ì¨ú¤@­Óºô­¶¡A¨Ã©â¨ú¥Xºô­¶¤º®eªº©Ò¦³³sµ²¡]¥\¯à¨Ã¤£§¹¥þ¡A¥i¦A§ï¶i¡I¡^
inet=new ActiveXObject("InetCtls.Inet");		// ¨ú±o Inet Control ª«¥ó
inet.Url="http://www.cs.nthu.edu.tw";			// ±ý¤U¸ü¤§ºô­¶
inet.RequestTimeOut=20;					// ³]©w¹Á¸Õ®É¶¡
WScript.Echo("Downloading \""+inet.Url+"\"...");
content = inet.OpenURL();				// ¤U¸üºô­¶
pattern=/<A(.*?)<\/A>/gi;				// ©w¸q³q¥Îªí¥Ü¦¡
found=content.match(pattern);				// §ì¥X³sµ²
pattern2=/<\s*A\s+HREF\s*=\s*"?(.*?)"?\s*>(.*?)<\s*\/\s*A\s*>/i;	// ¥t¤@­Ó³q¥Î¹Bºâ¦¡
for (i=0; i<found.length; i++){
	pattern2.exec(found[i]);		// §ì¥X³sµ²ªººô§}¥H¤Î³sµ²ªº¤å¦r
	WScript.Echo(found[i]+" ===> URL="+RegExp.$1+", TEXT="+RegExp.$2);
}

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì§Q¥Î WSH ¨Ó§ì¨ú²M¤j¸ê°T¨tªº­º­¶¡A¨Ã¨Ï¥Î³q¥Îªí¥Üªk¨Ó§ì¨ú³sµ²ºô§}¤Î³sµ²¤å¦r¡A¨å«¬¿é¥X¦p¤U¡G

Downloading "http://www.cs.nthu.edu.tw"... <a href="2003NTHU_CS_Chinese.doc" target="_blank">(¤¤)</a> ===> URL=2003NTHU_CS_Chinese.doc" target="_blank, TEXT=(¤¤) <a href="2003NTHU_CS_English.doc" target="_blank">(­^)</a> ===> URL=2003NTHU_CS_English.doc" target="_blank, TEXT=(­^) <a href="/intro.html"><img src="/icon/csbuild_8.jpg" lowsrc="/icon/csbuild_7b.jpg" border=0 alt="²MµØ¸ê°T²¥v"></a> ===> URL=/intro.html, TEXT=<img src="/icon/csbuild_8.jpg" lowsrc="/icon/csbuild_7b.jpg" border=0 alt="²MµØ¸ê°T²¥v"> <a href="Grading_report.doc">¨t©Ò¦Û§ÚµûŲ³ø§i</a> ===> URL=Grading_report.doc, TEXT=¨t©Ò¦Û§ÚµûŲ³ø§i <a href="mailto:www@cs.nthu.edu.tw">·N ¨£ »P «ü ±Ð</a> ===> URL=mailto:www@cs.nthu.edu.tw, TEXT=·N ¨£ »P «ü ±Ð <a href="/webteam">»s §@ ¤p ²Õ</a> ===> URL=/webteam, TEXT=»s §@ ¤p ²Õ <a href="special.html"><font face="Arials">¾Ç ³N ¨ô ¶V & ªÀ °Ï Ãö Ãh</a> ===> URL=special.html, TEXT=<font face="Arials">¾Ç ³N ¨ô ¶V & ªÀ °Ï Ãö Ãh ... «e­±½d¨Ò©Ò¥Îªº InetCtls.Inet ¤¸¥ó¡A¤ñ¸û²³æ¡A©Ò¥HµLªk°»´úºô¸ôÂ_½uªº±¡ªp¡A¥t¤@­Ó IIS ¤º«Øªº¤¸¥ó WinHttp.WinHttpRequest¡A«h¥i¥H¦³¸û¦h°»¿ù¥\¯à¡A¥H¤U½d¨Ò¥i¥H§ì¨ú Google ­º­¶¡G

­ì©lÀÉ¡]getWebPage05.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// §ì¨ú¤@­Óºô­¶¨ÃÅã¥Ü¨ä¤º®e
url="http://www.google.com";
try {
	WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
	WinHttpReq.Open("GET", url, false);
	WinHttpReq.Send();
	result = WinHttpReq.ResponseText;
} catch (objError) {
	result = objError+"\n";
	result += ("objError.number = "+(objError.number & 0xFFFF).toString()+"\n");
	result += ("objError.description = "+objError.description);
}
WScript.Echo(result);

ŪªÌ¥i¥H¼È°±ºô¸ô¡A¦A¸Õ¬Ý¬Ý¤W­z½d¨Ò¡A´N·|¦L¥X§ì¤£¨ìºô­¶ªº¿ù»~°T®§¤F¡C

§A¤]¥i¥H¨Ï¥Î¥t¤@­Ó¤º«Ø¤¸¥ó XMLHTTP ¨Ó§ì¨úºô­¶¡G

­ì©lÀÉ¡]getUrl.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
url = "http://www.google.com.tw";
objHttp = new ActiveXObject("Microsoft.XMLHTTP");
objHttp.open("GET", url, false, "");
objHttp.send();
content = objHttp.responseText;
WScript.Echo(content);

§Ú­Ì¤]¥i¥H§ì¨ú½Ñ¦p jpg ©Î mid µ¥¤G¶i¨îÀɮסG

­ì©lÀÉ¡]getBinaryFile.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// Get Binary File from Web
// Usage: cscript getBinaryFile.js url output
// Example: cscript getBinaryFile.js http://neural.cs.nthu.edu.tw/jang/books/javascript/example/image/19980425/0041.jpg annie.jpg
// Roger Jang, 20081215, tested under Vista

args = WScript.Arguments;
if (args.Count()!=2) {
	WScript.Echo("Usage: cscript " + WScript.ScriptName + " url output");
	WScript.Echo("For example:");
	WScript.Echo("\tcscript " + WScript.ScriptName + " \"http://neural.cs.nthu.edu.tw/jang/books/javascript/example/image/19980425/0041.jpg\" \"annie.jpg\"");
	WScript.Quit();
}

url = args(0);
output = args(1);
getBinaryFile(url, output);

function getBinaryFile(url, output){

		// ====== ¨Ï¥Î Microsoft.XMLHTTP §ì¨úÀɮסC
		http = WScript.CreateObject("Msxml2.ServerXMLHTTP.3.0");
		http.open("GET", url, false);
		WScript.Echo("Retrieving file at " + url);
		http.send();
		statusCode = http.status;
		if (statusCode != 200){			// Àɮפ£¥¿±`
			WScript.Echo("Error in retrieving file at " + url);
			WScript.Echo("Status code = " + statusCode);
			WScript.Quit();
		}
		file = http.responseBody;
		BinaryStream = WScript.CreateObject("ADODB.Stream");
		BinaryStream.type = 1;
		BinaryStream.open();
		// ====== Àx¦sÀÉ®×
		WScript.Echo("Saving file to " + output + "...");
		BinaryStream.write(file);
		BinaryStream.saveToFile(output, 2);
		WScript.Echo("Done.");
}


JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó³æ¾÷ªº WSH Àô¹Ò