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();
}
}
}
(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();
}
}
}
相关文章推荐
- C#用HttpWebRequest通过代理服务器验证后抓取网页内容
- .Net/C# 应用程序直接读取本地 Cookies 文件 (由于 WinXP SP2 调用 API: InternetGetCookie 无果)
- 使用http原生包抓取RestAPI接口内容并解析为实体的示例
- C#用HttpWebRequest通过代理服务器验证后抓取网页内容
- C#用HttpWebRequest通过代理服务器验证后抓取网页内容
- C# 调用jave中ajax的WebService引用后添加HTTP Header的问题
- C#用HttpWebRequest通过代理服务器验证后抓取网页内容
- .Net/C# 应用程序直接读取本地 Cookies 文件(WinXP SP2 调用 API: InternetGetCookie 无果)
- 【转载】C# HttpHelper,Httprequest,请求时无视编码,无视证书,无视Cookie,网页抓取
- C# HttpHelper,Httprequest,请求时无视编码,无视证书,无视Cookie,网页抓取
- C#用HttpWebRequest通过代理服务器验证后抓取网页内容 。。。。。
- .Net/C# 应用程序直接读取本地 Cookies 文件 (由于 WinXP SP2 调用 API: InternetGetCookie 无果)
- C# HttpHelper帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取
- C#如何使用HttpWebRequest、HttpWebResponse模拟浏览器抓取网页内容
- C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取
- 【转】C#用HttpWebRequest通过代理服务器验证后抓取网页内容
- C# HttpHelper帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取
- C#用HttpWebRequest通过代理服务器验证后抓取网页内容
- C#用HttpWebRequest通过代理服务器验证后抓取网页内容