如何使用 WebBrowser 控件注册自己的协议? [英] How to register own protocol using the WebBrowser control?

查看:23
本文介绍了如何使用 WebBrowser 控件注册自己的协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有 WebBrowser 控件的 WP7 Silverlight 应用程序中,我想使用自己的协议(如myttp://")来提供一些本地内容.我无法将 Navigate() 用于隔离存储,因为某些内容将按需创建.出于同样的原因,NavigateToString() 也不适用于我.

In a WP7 Silverlight application with a WebBrowser control I want to use an own protocol like "myttp://" to deliver some local content. I can't use Navigate() to an IsolatedStrorage because some content will by created on demand. For the same reason NavigateToString() is also not usable for me.

我试图为我的 MYTP 协议注册一个 WebRequestCreator 下降

I tried to register a WebRequestCreator descend for my MYTP protocol

myCreator = new MyRequestCreator(); 
WebRequest.RegisterPrefix("mytp://", myCreator);

但如果我导航到mytp://test.html",它不会从浏览器控件中调用.如果我通过代码创建一个 WebRequest

but it isn't called from the browser control if I navigate to "mytp://test.html". If I create a WebRequest via code

WebRequest request;
request = WebRequest.Create("mytp://test.html");`

一切正常.

任何建议有什么问题或如何做?

Any suggestions what is wrong or how to do it?

推荐答案

我同意 AnthonyWJones 的话,虽然我不知道他所说的浏览器 HTTP 堆栈"究竟是什么意思.

I agree with AnthonyWJones words, though I dont know, what exactly he meant by "Browser HTTP stack".

标准 Silverlight 以 System.Net.Browser.WebRequestCreator.BrowserHttp httprequest 工厂的形式访问浏览器堆栈"(用于处理会话等)(相对于正常/旁边" System.Net.Browser.WebRequestCreator.ClientHttpfactory) 实际上可用于 WP7 中的应用程序代码.它对 SDK 是隐藏的,但可以在设备上使用,只需很少的努力,应用程序就可以使用它,例如,使其发出的 cookie 与浏览器的缓存同步.有关说明,请参阅我的其他帖子

The standard Silverlight's "access to Browser's stack" (used to handle sessions etc) in form of System.Net.Browser.WebRequestCreator.BrowserHttp httprequest factory (versus the "normal/aside" System.Net.Browser.WebRequestCreator.ClientHttp factory) is actually available to the application code in WP7. It is hidden from the SDK, but available on the device and with small effort, the application can use it, for example, to have its emitted cookies in sync with the Browser's cache. For description, please see my humble other post

然而,在使用该工厂并在这些连接中与 WebBrowser 同步处理所有会话/cookies/userauth 时,尽管与 ClientHttp 工厂非常相似,但您发现(至少在 7.0 和 7.1 版本中)它完全不知道任何自定义前缀.试图用这个工厂打开任何东西都会导致 (WP7 v. Mango 7.1):

However, while using that factory and having all your session/cookies/userauth handling within those connections in sync with the WebBrowser, despite being very similar to the ClientHttp factory, you find (at least in 7.0 and 7.1 versions) that it is completely ignorant of any custom prefixes. Trying to open anything with this factory results in (WP7 v. Mango 7.1):

A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.Windows.dll
at System.Net.Browser.BrowserHttpWebRequest.InternalBeginGetRequestStream(AsyncCallback callback, Object state)
at System.Net.Browser.AsyncHelper.BeginOnUI(BeginMethod beginMethod, AsyncCallback callback, Object state)
at System.Net.Browser.BrowserHttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state)
at MyApp.MyPage..ctor()

MyPage 的相关代码片段:

relevant code snippet of the MyPage:

public class WRC : IWebRequestCreate { public WebRequest Create(Uri uri) { return null;/*BREAKPOINT1*/ } }

WebRequest.RegisterPrefix("js://", new WRC()); // register the above handler

brwHttp = (IWebRequestCreate)typeof(System.Net.Browser.WebRequestCreator).GetProperty("BrowserHttp").GetValue(null, null);
var tmp = brwHttp.Create(new Uri("js://blah.blah.blah"));

var yyy = tmp.BeginGetResponse(callback, "wtf");
var response = tmp.EndGetResponse(yyy); /*BREAKPOINT2*/

var zzz = tmp.BeginGetRequestStream(callback, "wtf"); /*<---EXCEPTION*/
var stream = tmp.EndGetRequestStream(zzz); /*BREAKPOINT3*/

执行结果:

  • breakpoint1 从未命中
  • breakpoint2 允许看到响应"为 NULL
  • 由于上面粘贴的异常,breakpoint3 从未命中

我的结论是,Silverlight 浏览器的堆栈被硬编码为使用一些内置前缀集,而所有其他前缀都被忽略/抛出 ProtocolViolation.我的猜测是,在 WP7 (7.0, 7.1) 中,它们实际上被硬编码为使用 http,因为我的自定义js://"已传递给 BrowserHttpWebRequest.InternalBeginGetRequestStream,因为它在堆栈跟踪中可见 :)

My conclusion is, that the Silverlight Browser's stack is hardcoded to use some builtin set of prefixes, and all other prefixes are ignored/throw ProtocolViolation. My guess is, that in WP7 (7.0, 7.1) they are actually hardcoded to use http since my custom "js://" was passed to a BrowserHttpWebRequest.InternalBeginGetRequestStream as it's visible on the stacktrace :)

这证实了 Anthony 所写的内容 - 没有办法让自定义协议处理程序与 Silverlight 的浏览器堆栈 API 正常工作.

That confirms what Anthony had written - no way of having custom protocol handlers to work gracefully with the Silverlight's Browser Stack API.

但是,我不能同意 WebBrowser 使用此连接工厂.虽然隐藏工厂被称为 BrowserHttp 是真的,并且它确实与 webbrowser 共享一些每个用户或每个会话的设置,但我尝试的一切都表明 WebBrowser 组件完全使用其他工厂进行连接,很可能它是一些本土的.作为一个论据,我只能提供我能够用我的简单自定义实现(在模拟器和手机上)成功替换原始 BrowserHttp 工厂,并且在我当前的应用程序中至少有 6 个网络浏览器,它根本没有用过,甚至一次都没有!(既不在模拟器上,也不在手机上)

However, I cannot agree with that the WebBrowser uses this connection factory. While is it true that the hidden factory is called BrowserHttp, and is true that it shares some per-user or per-session settings with the webbrowser, everything I try tens to indicate that the WebBrowser component uses yet completly other factory for its connections, and quite probably it is some native one. As an argument for that, I can only provide that I was able to successfully replace the original BrowserHttp factory with my simple custom implementation of it (both on the emulator and the phone), and with at least 6 webbrowsers in my current app, it wasn't used at all, not even once! (neither on the emulator, nor phone)

这篇关于如何使用 WebBrowser 控件注册自己的协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆