找了整整两天,在大海捞针之后,终于在上古代码中找到了一个可用的。。。。
可以用喜极而泣来形容测试有效之后的心情!!!
跟cefsharp相比geckofx最大的优点就是模式更接近于IE,包括node、xml、xpath以及单独设置cookie等。但是资料、问答等实在是太古老太古老了。
如果已入坑geckofx的朋友,请一定要记住,遇到问题先不用去满世界搜索,请第一时间去官网的 Issues 版块搜索关键字。
具体方法:https://bitbucket.org/geckofx/ -> 选择版本 ->左侧点击 Issues -> 右侧 Find Issues -> 右键谷歌翻译网页
最好每个版本里面都进去搜一下,我就是在上古时代的29.0里搜到的答案
文章原地址:https://bitbucket.org/geckofx/geckofx-29.0/issues/197/getting-content-on-observer-response
一、拦截响应:
1、具体代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gecko.Net;
using Gecko.Observers;
namespace GeckofxWebBrowser.GeckofxExtension
{
public class MyObserver : BaseHttpModifyRequestObserver
{
public delegate void TicketLoadedEventHandler(HttpChannel p_HttpChannel, object sender, System.EventArgs e);
/// <summary>
/// 处理事件委托
/// </summary>
public event TicketLoadedEventHandler TicketLoadedEvent;
/// <summary>
/// 需要拦截的网址,只要独一无二的关键词既可
/// </summary>
public List<string> targetUrls = new List<string>() { };
protected override void ObserveRequest(HttpChannel p_HttpChannel)
{
if (p_HttpChannel != null)
{
if (targetUrls.Any(s => p_HttpChannel.Uri.AbsoluteUri.Contains(s)))
{
TraceableChannel oTC = p_HttpChannel.CastToTraceableChannel();
StreamListenerTee oStream = new StreamListenerTee();
oStream.Completed += (sender, e) => TicketLoadedEvent(p_HttpChannel, sender, e);
oTC.SetNewListener(oStream);
}
}
}
//********************* TicketLoadedEvent 事件处理参考 *********************
/// <summary>
/// 主要是如何读取response数据,p_HttpChannel内可获取url、header等,header内应该也包含setcookie,没去测试
/// </summary>
/// <param name="p_HttpChannel"></param>
/// <param name="sender"></param>
/// <param name="e"></param>
//private void MyObserver_TicketLoadedEvent(HttpChannel p_HttpChannel, object sender, EventArgs e)
// {
// if (sender is StreamListenerTee)
// {
// StreamListenerTee oStream = sender as StreamListenerTee;
// byte[] aData = oStream.GetCapturedData();
// string sData = Encoding.UTF8.GetString(aData);
// }
// }
}
}
2、使用方法:将拦截类加入到观察器
MyObserver MyObs = new MyObserver();
MyObs.TicketLoadedEvent += MyObs_TicketLoadedEvent;//如何处理捕捉到的response
MyObs.targetUrls = new List<string>() {“baidu.com”,“163.com” };//添加要拦截的url关键词
ObserverService.AddObserver(MyObs);//添加观察器
private void MyObs_TicketLoadedEvent(Gecko.Net.HttpChannel p_HttpChannel, object sender, EventArgs e)
{
if (sender is StreamListenerTee)
{
StreamListenerTee oStream = sender as StreamListenerTee;
byte[] aData = oStream.GetCapturedData();
string sData = Encoding.UTF8.GetString(aData);
}
}
二、如果要拦截请求
//在初始化的时候先设置这两样
browser.UseHttpActivityObserver = true;
browser.ObserveHttpModifyRequest += Browser_ObserveHttpModifyRequest;//
private void Browser_ObserveHttpModifyRequest(object sender, GeckoObserveHttpModifyRequestEventArgs e)
{
//在这里通过 e 的各项属性做具体处理,可以判断url,可以判断标头、包括post内容等
}
---------------------
作者:jwb7832007
来源:CSDN
原文:https://blog.csdn.net/jwb7832007/article/details/80884128
版权声明:本文为博主原创文章,转载请附上博文链接!
出处:http://www.cnblogs.com/huangcong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。