您的位置:首页 > 理论基础 > 计算机网络

C#调用Httpwatch API 抽取抓取日志、Header、Cookie等内容

2016-12-12 14:39 716 查看
(1)安装httpwatch; 

(2)打开c#工程,在“引用”中增加“COM”组件,在“COM”组件中找到“HttpWatch Professional * Automation Libary”,确定。(*为安装的httpwatch版本) 

(3)代码如下,具体API请参考HttpWatch
Automation Reference

using System;
using HttpWatch;

namespace page_check
{
class PageChecker
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Enter the URL of the page to check (press enter for http://www.baidu.com/):\r\n"); string url = Console.ReadLine();
if ( url.Length == 0 )
url = "http://www.baidu.com/";

Console.WriteLine("\r\nChecking " + url + "...\r\n");

// Create a new instance of HttpWatch in IE
Controller control = new Controller();
Plugin plugin = control.IE.New();

// Start Recording HTTP traffic
plugin.Log.EnableFilter(false);
plugin.Record();

// Goto to the URL and wait for the page to be loaded
plugin.GotoURL(url);
control.Wait(plugin, -1);

// Stop recording HTTP
plugin.Stop();

if (plugin.Log.Pages.Count != 0)
{
Console.WriteLine("\r\nPage Title: '" + plugin.Log.Pages[0].Title + "'");

Console.WriteLine();

// Display summary statistics for page
Summary summary = plugin.Log.Pages[0].Entries.Summary;
Console.WriteLine("Total time to load page (secs): " + summary.Time);
Console.WriteLine("Number of bytes received on network: " + summary.BytesReceived);
Console.WriteLine("HTTP compression saving (bytes): " + summary.CompressionSavedBytes);
Console.WriteLine("Number of round trips: " + summary.RoundTrips);
Console.WriteLine("Number of errors: " + summary.Errors.Count);

Console.WriteLine("-----------------------",plugin.Log.Entries.Count);

//Entries e = plugin.Log.Entries;
for (int i=0; i<plugin.Log.Entries.Count; i++)
{
Console.Write(plugin.Log.Entries[i].URL);
Console.WriteLine(" " + plugin.Log.Entries[i].Time);
//如下是访问Header、Cookie、Post Data等代码
Headers headers = plugin.Log.Entries[i].Headers;
for(int j=0;j<headers.Count;j++)
{
Header header = headers[j];
Console.WriteLine("Header."+header.Name+" = "+header.Value);
}
}
}
// Close down IE
plugin.CloseBrowser();

Console.WriteLine( "\r\nPress Enter to exit");
Console.ReadLine();
}
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐