PB中怎么通过http协议发包和接收包,加

PB中怎么通过http协议发包和接收包,加,第1张

你所指的“网页链接”应该是指WebService吧?这个需要用xml来进行数据的交互,具体例子我手头上没有了,只记得需要把pbdom##pbd和pbsoapclient##pbd关联到你的工作区中(##为PB版本号)

如果使用Winform的WebBrowser控件,我们可以监听它的NewWindow事件,在这个事件中做一些处理,例如,在新建一个Tab来打开,或者控制它在当前WebBrowser中跳转。很不幸的是,WPF的WebBrowser没有这个事件。

说到底,Winform的WB或者是WPF的WB都是在调用IE的一个控件,因此,Winform能加上的,我们WPF一定也有办法加上。如此,那我们就请出神器Reflector,研究一把。

首先,我们打开Winform的WebBrowser,找到触发NewWindow事件的代码

protected virtual void OnNewWindow(CancelEventArgs e)

{

if (thisNewWindow != null)

{

thisNewWindow(this, e);

}

}

它是在OnNewWindow方法中触发的。那么,是谁调用了这个OnNewWindow呢?接着搜索,最后在一个叫WebBrowserEvent的类里面发现这么一段:

public void NewWindow2(ref object ppDisp, ref bool cancel)

{

CancelEventArgs e = new CancelEventArgs();

thisparentOnNewWindow(e);

cancel = eCancel;

}

我们接着搜NewWindow2,却发现没有地方显式地调用它了。既然从方法入手没找到,那我们就来研究一下定义这个方法的WebBrowserEvent,看看是谁在使用它。

仔细搜索一遍,最后发现在WebBrowser的CreateSink方法中有这么一段:

代码

protected override void CreateSink()

{

object activeXInstance = baseactiveXInstance;

if (activeXInstance != null)

{

thiswebBrowserEvent = new WebBrowserEvent(this);

thiswebBrowserEventAllowNavigation = thisAllowNavigation;

thiscookie = new AxHostConnectionPointCookie(activeXInstance, thiswebBrowserEvent, typeof(UnsafeNativeMethodsDWebBrowserEvents2));

}

}

注意这句话:

thiscookie = new AxHostConnectionPointCookie(activeXInstance, thiswebBrowserEvent, typeof(UnsafeNativeMethodsDWebBrowserEvents2));

很显然,这句话是关键。AxHostConnectionPointCookie类的作用是:“将一个ActiveX 控件连接到处理该控件的事件的客户端”。

上面的调用中有一个很奇怪的类型:DWebBrowserEvents2,熟悉COM的童鞋应该马上能想到,这其实是一个COM类型的定义。

代码

[ComImport, TypeLibType(TypeLibTypeFlagsFHidden), InterfaceType(ComInterfaceTypeInterfaceIsIDispatch), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D")]

public interface DWebBrowserEvents2

{

}

实际上,我们再去看WebBrowserEvent的定义,它恰恰是实现了这个接口的。

[ClassInterface(ClassInterfaceTypeNone)]

private class WebBrowserEvent : StandardOleMarshalObject, UnsafeNativeMethodsDWebBrowserEvents2

{

}

因此,上面这句话不难理解,就是定义一个实现了特定COM接口的类型,让浏览器控件的事件能够转发到这个类型实例去处理。因此,NewWindow2其实是浏览器控件去调用的。

Winform的WebBrowser我们搞清楚了,下面我们来看WPF的。其实,打开WPF的WebBrowser代码之后,我们会发现它跟Winform的WebBrowser机制是一样的。一个似曾相识的CreateSink方法映入眼中:

代码

[SecurityTreatAsSafe, SecurityCritical]

internal override void CreateSink()

{

this_cookie = new ConnectionPointCookie(this_axIWebBrowser2, this_hostingAdaptorCreateEventSink(), typeof(UnsafeNativeMethodsDWebBrowserEvents2));

}

这儿也有一个ConnectionPointCookie,但是它的访问权限是internal的:(

第二个参数,_hostingAdapterCreateEventSink返回的是什么呢:

代码

[SecurityCritical]

internal virtual object CreateEventSink()

{

return new WebBrowserEvent(this_webBrowser);

}

[ClassInterface(ClassInterfaceTypeNone)]

internal class WebBrowserEvent : InternalDispatchObject<UnsafeNativeMethodsDWebBrowserEvents2>, UnsafeNativeMethodsDWebBrowserEvents2

{

}

仍然是一个WebBrowserEvent!悲剧的是,这个WPF的WebBrowserEvent,并没有触发NewWindowEvent:

public void NewWindow2(ref object ppDisp, ref bool cancel)

{

}

现在知道为什么WPF的WB控件没有NewWindow事件了吧?微软的童鞋压根儿就没写!

既然微软的童鞋不写,那我们就自己折腾一把,反正原理已经搞清楚了。

首先,我们也得定义一个DWebBrowserEvents2接口,这个我们直接通过Reflector复制一份就好了。代码就不贴上来了。

接着,我们再仿造一个WebBrowserEvent,关键是要触发NewWindow事件:

代码

public partial class WebBrowserHelper

{

private class WebBrowserEvent : StandardOleMarshalObject, DWebBrowserEvents2

{

private WebBrowserHelper _helperInstance = null;

public WebBrowserEvent(WebBrowserHelper helperInstance)

{

_helperInstance = helperInstance;

}

public void NewWindow2(ref object pDisp, ref bool cancel)

{

_helperInstanceOnNewWindow(ref cancel);

}

}

}

最后,我们需要仿造Framework中的代码,也来CreateSink一把(我承认,用了反射来取WebBrowser内部的东东,谁让这些类型都是internal的呢):

代码

private void Attach()

{

var axIWebBrowser2 = _webBrowserReflectGetProperty("AxIWebBrowser2");

var webBrowserEvent = new WebBrowserEvent(this);

var cookieType = typeof(WebBrowser)AssemblyGetType("MSInternalControlsConnectionPointCookie");

_cookie = ActivatorCreateInstance(

cookieType,

ReflectionServiceBindingFlags,

null,

new[] { axIWebBrowser2, webBrowserEvent, typeof(DWebBrowserEvents2) },

CultureInfoCurrentUICulture);

}

最后的使用:

var webBrowserHelper = new WebBrowserHelper(webBrowser);

webBrowserHelperNewWindow += WebBrowserOnNewWindow;

效果图

初始网页:

点击一个链接,默认情况下,将是d出一个IE窗口,现在是在新的Tab中打开:

以上就是关于PB中怎么通过http协议发包和接收包,加全部的内容,包括:PB中怎么通过http协议发包和接收包,加、PB OLE如何监听网页某个隐藏按钮的点击事件、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10139350.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存