HttpWatch提供了一些脚本示例代码供大家参考,在HttpWatch安装目录下的“api_examples”文件夹下,您会看到2个子文件夹:“ie”和“Firefox”,这里我们希望看到基于IE浏览器的相关API调用方法,所以选择“ie”文件夹,再进入到“page_check”,在该文件夹下还有4个子文件夹,分别是“csharp”、“javascript”、“ruby”、“VBScript”,下面就让我们一起来看一下这4个文件夹下主要的脚本实现代码。
Ruby脚本代码(和文件内容):
#文件 # Page Check Ruby Example # # For more information about this example please refer to the readme.txt file in the # same directory # require 'win32ole' puts "Enter the URL of the page to check (press enter for www.):\n"; $stdout. flush url = ge! if url.empty? url = "www." end puts "\nChecking " + url + "...\r\n\r\n"; $ # Create a new instance of HttpWatch in IE control = WIN32OLE.new('H;) plugin = con() # Start Recording HTTP traffic (false) () # Goto to the URL and wait for the page to be loaded (url) con(plugin, -1) # Stop recording HTTP () if != 0 printf "\nPage Title: '%s'\n", (0).Title # Display summary statistics for page summary = (0).En printf "Total time to load page (secs): %.3f\n", printf "Number of bytes received on network: %d\n", printf "HTTP compression saving (bytes): %d\n", printf "Number of round trips: %d\n", printf "Number of errors: %d\n", end # Close down IE () puts "\r\nPress Enter to exit"; $ gets # 文件 # Page Check Ruby and WATIR Example # # For more information about this example please refer to the readme.txt file in the # same directory # require 'win32ole' require 'watir' puts "Enter the URL of the page to check (press enter for www.):\n"; $stdout. flush url = ge! if url.empty? url = "www." end puts "\nChecking " + url + "...\r\n\r\n"; $ # Attach HttpWatch control = WIN32OLE.new('H;) # Open the IE browser plugin = con # Attach Watir to IE browser containing HttpWatch plugin Watir::IE::attach_timeout = 30 ie = Watir::IE.attach(:hwnd, ) # Start Recording HTTP traffic () (false) () # Goto to the URL and wait for the page to be loaded ie.goto(url) # Stop recording HTTP () if != 0 printf "\nPage Title: '%s'\n", (0).Title # Display summary statistics for page summary = (0).En printf "Total time to load page (secs): %.3f\n", printf "Number of bytes received on network: %d\n", printf "HTTP compression saving (bytes): %d\n", printf "Number of round trips: %d\n", printf "Number of errors: %d\n", end # Close down IE (); puts "\r\nPress Enter to exit"; $ gets
C#源代码(文件内容):
// Page Check C# Example // // For more information about this example please refer to the readme.txt file in the // same directory // using System; using HttpWatch; namespace page_check { class PageChecker { [STAThread] static void Main(string[] args) { Con("Enter the URL of the page to check (press enter for www.):\r\n"); string url = Con(); if ( url.Length == 0 ) url = "www."; Con("\r\nChecking " + url + "...\r\n"); // Create a new instance of HttpWatch in IE Controller control = new Controller(); Plugin plugin = con(); // Start Recording HTTP traffic (false); (); // Goto to the URL and wait for the page to be loaded (url); con(plugin, -1); // Stop recording HTTP (); if ( != 0) { Con("\r\nPage Title: '" + [0].Title + "'"); Con(); // Display summary statistics for page Summary summary = [0].En; Con("Total time to load page (secs): " + ); Con("Number of bytes received on network: " + ); Con("HTTP compression saving (bytes): " + ); Con("Number of round trips: " + ); Con("Number of errors: " + ); }
VBScript脚本代码(文件内容):
' Page Check VBScript Example ' ' For more information about this example please refer to the readme.txt file in the ' same directory ' WScri(vbCrLf & "Enter the URL of the page to check (press enter for www.):" & vbCrLf) Dim url url = WScri if Len(url) = 0 then url = "www." end if WScri( vbCrLf & "Checking " & url & "..." & vbCrLf) ' Create a new instance of HttpWatch in IE Dim control Set control = CreateObject("H;) Dim plugin Set plugin = con ' Start Recording HTTP traffic false ' Goto to the URL and wait for the page to be loaded url con plugin, -1 ' Stop recording HTTP if <> 0 then WScri("") WScri("Page Title: '" & (0).Title & "'") ' Display summary statistics for page Dim summary Set summary = (0).En WScri( "Total time to load page (secs): " & ) WScri( "Number of bytes received on network: " & ) WScri( "HTTP compression saving (bytes): " & ) WScri( "Number of round trips: " & ) WScri( "Number of errors: " & ) end if ' Close down IE WScri( vbCrLf & "Press Enter to exit") WScri
JavaScript脚本代码(文件内容):
// Page Check Javascript Example // // For more information about this example please refer to the readme.txt file in the // same directory // WScri("\nEnter the URL of the page to check (press enter for www.):\n"); var url = WScri(); if ( url.length == 0 ) url = "www."; WScri("\nChecking " + url + "...\n"); // Create a new instance of HttpWatch in IE var control = new ActiveXObject('H;); var plugin = con(); // Start Recording HTTP traffic (false); (); // Goto to the URL and wait for the page to be loaded (url); con(plugin, -1); // Stop recording HTTP (); if ( != 0 ) { WScri( "\nPage Title: '" + (0).Title + "'"); // Display summary statistics for page var summary = (0).En; WScri( "Total time to load page (secs): " + ); WScri( "Number of bytes received on network: " + ); WScri( "HTTP compression saving (bytes): " + ); WScri( "Number of round trips: " + ); WScri( "Number of errors: " + ); } // Close down IE (); WScri( "\r\nPress Enter to exit"); WScri();
上述4个脚本其实现的目标是一致的,都是让您先输入一个要考察的网址,然后其调用httpwatch相关的API函数开始录制、记录过程数据、停止录制、输出结果信息、关闭浏览器这样一个处理过程。这里以JavaScript脚本为例,运行“run.cmd”文件,在弹出的控制台界面输入“www.baidu.com”,将会自动打开“百度”页面,过一会页面将会关闭返回控制台界面,在控制台将产生相关的结果信息,如图14-22所示,按任意键将退出控制台界面。
图14-22 JavaScript脚本执行后结果输出信息