标签: webbrowser-control

WebBrowser控件和嵌入标签

我相信我遇到了与在我的C#2008 WinForms应用程序中使用带有WebBrowser控件的embed标签相关的安全问题.

这是我的代码:

private void button2_Click(object sender, EventArgs e)
{
    webBrowser1.Navigate("C:/page1.html");
}

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Navigate("about:blank");
    Thread.Sleep(1000);
    webBrowser1.Document.Write("<html><body><p>Hello</p><embed src='C:/test.wmv' /></body></html>");
}
Run Code Online (Sandbox Code Playgroud)

这是page1.html的内容:

<html><body><p>Hello</p><embed src='C:/test.wmv' /></body></html>
Run Code Online (Sandbox Code Playgroud)

Button1生成单词"Hello".Button2生成单词"Hello",其下方嵌入了一个电影播放器​​.

当我查看两个页面的源时,我注意到它们是相同的,除了源文件的名称.

这让我相信它与我的IE安全设置有关,但我注意到我为嵌入式内容设置了完整的权限.也许控件不能正确识别页面的来源,因此不允许使用嵌入标签.

我怎样才能以编程方式克服这个问题?我想避免将我的页面写入文件,并不惜一切代价导航到该文件.有关如何欺骗浏览器控件正常工作的任何建议?

第一编辑:

根据这篇文章Webbrowser Navigate Embedded Resource这将有效,但是我(JT)尝试了它并没有:

System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication1.Properties.test.html");
webBrowser1.DocumentStream = stream;
Run Code Online (Sandbox Code Playgroud)

再现问题时的奇怪行为:

webBrowser1.Navigate("about:blank");
do
{
Thread.Sleep(100);
} while (webBrowser1.IsBusy == true);

//Method 1. Doesn't work
string htmlString1 = File.ReadAllText("C:/page1.html");
webBrowser1.Document.Write(htmlString1);

//Method 2. Doesn't work
string htmlString2 = "<html><body><p>Hello</p><embed src='C:/test.wmv' /></body></html>";
webBrowser1.Document.Write(htmlString2);

//Method 3. DOES …
Run Code Online (Sandbox Code Playgroud)

c# webbrowser-control winforms

13
推荐指数
1
解决办法
4233
查看次数

如何在超时期限后取消任务等待

我正在使用此方法以编程方式实例化Web浏览器,导航到URL并在文档完成时返回结果.

如果文档加载时间超过5秒,我将如何停止TaskGetFinalUrl()返回null

我见过许多使用a的例子,TaskFactory但我无法将其应用于此代码.

 private Uri GetFinalUrl(PortalMerchant portalMerchant)
    {
        SetBrowserFeatureControl();
        Uri finalUri = null;
        if (string.IsNullOrEmpty(portalMerchant.Url))
        {
            return null;
        }
        Uri trackingUrl = new Uri(portalMerchant.Url);
        var task = MessageLoopWorker.Run(DoWorkAsync, trackingUrl);
        task.Wait();
        if (!String.IsNullOrEmpty(task.Result.ToString()))
        {
            return new Uri(task.Result.ToString());
        }
        else
        {
            throw new Exception("Parsing Failed");
        }
    }

// by Noseratio - http://stackoverflow.com/users/1768303/noseratio    

static async Task<object> DoWorkAsync(object[] args)
{
    _threadCount++;
    Console.WriteLine("Thread count:" + _threadCount);
    Uri retVal = null;
    var wb = new WebBrowser();
    wb.ScriptErrorsSuppressed = …
Run Code Online (Sandbox Code Playgroud)

.net c# console-application webbrowser-control task-parallel-library

13
推荐指数
1
解决办法
2万
查看次数

在新TAB中打开链接(WebBrowser控件)

有人知道如何在WinForms应用程序中单击WebBrowser控件中的链接,然后在我的TabControl中的新选项卡中打开该链接吗?

我已经搜索了几个月,看过很多教程/文章/代码示例,但似乎没有人曾经在C#中尝试过这个.

任何建议/样品都非常感谢.

谢谢.

.net c# tabcontrol webbrowser-control winforms

12
推荐指数
1
解决办法
6万
查看次数

WebBrowser控件页面加载错误

我的winform上有WebBrowser控件.当我尝试导航到某个网站时,我得到标准的IE错误页面,如:

  • "导航到网页已被取消"
  • "地址无效"
  • "页面无法加载"
  • 等等

我需要处理这些错误并将自定义错误消息返回给用户.有什么方法可以解决这个问题吗?

.net c# webbrowser-control winforms

12
推荐指数
1
解决办法
1万
查看次数

我可以在我的webapp中禁用浏览器刷新吗?

我想在我的网站上禁用刷新.有没有办法在网页中禁用F5,Ctrl- R或重新加载?

html javascript browser webbrowser-control

12
推荐指数
3
解决办法
4万
查看次数

如何将数组从C#传递给Javascript函数?

我使用WPF中的WebBrowser对象,我在浏览器中加载的页面中调用了一些Javascript代码,如下所示:

myWebBrowser.InvokeScript("myJsFunc", new object[] { foo.Text, bar.ToArray<string>()});
Run Code Online (Sandbox Code Playgroud)

现在,js函数应该迭代第二个参数(一个字符串数组)的元素并相应地做东西.唯一的问题是该参数似乎不会作为js数组传递.

例如,

alert(typeof theArray);
Run Code Online (Sandbox Code Playgroud)

提醒"未知".

从CSharp调用js函数时,将数组作为参数传递的正确方法是什么?

javascript c# webbrowser-control

12
推荐指数
2
解决办法
9909
查看次数

在WPF框架上设置ScrollViewer(用于垂直滚动)的正确方法?

有没有人知道在框架上定义垂直滚动条之间的区别如下:

        <ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto">
            <Frame Name="Frame1"
                   ScrollViewer.CanContentScroll="True" />
        </ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

或者像这样:

        <ScrollViewer Grid.Row="2">
            <Frame Name="Frame1"
                   ScrollViewer.VerticalScrollBarVisibility="Auto"
                   ScrollViewer.CanContentScroll="True" />
        </ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

此框架嵌套在WebBrowser控件中,并以第一种方式设置它正确显示垂直滚动条,并且仅在需要滚动(自动)时才可见.当我以第二种方式设置垂直滚动条时,即使它不需要滚动(可见)也始终可见.

我将使用第一个选项,因为它符合我的需要,但如果我设置错误,我不想在路上感到惊讶.

谢谢!

wpf scrollbar frame webbrowser-control scrollviewer

12
推荐指数
1
解决办法
5万
查看次数

如何使用.NET的WebBrowser或mshtml.HTMLDocument动态生成HTML代码?

我读过的关于这个主题的大多数答案都指向System.Windows.Forms.WebBrowser类或来自Microsoft HTML Object Library程序集的COM接口mshtml.HTMLDocument.

WebBrowser类没有引导我到任何地方.以下代码无法检索由我的Web浏览器呈现的HTML代码:

[STAThread]
public static void Main()
{
    WebBrowser wb = new WebBrowser();
    wb.Navigate("https://www.google.com/#q=where+am+i");

    wb.DocumentCompleted += delegate(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb.Document.DomDocument;
        foreach (IHTMLElement element in doc.all)
        {
                    System.Diagnostics.Debug.WriteLine(element.outerHTML);
        }     
    };
    Form f = new Form();
    f.Controls.Add(wb);
    Application.Run(f);
} 
Run Code Online (Sandbox Code Playgroud)

以上只是一个例子.我真的不想找到一个解决方法来找出我所在城镇的名称.我只需要了解如何以编程方式检索那种动态生成的数据.

(调用新的System.Net.WebClient.DownloadString(" https://www.google.com/#q=where+am+i "),将生成的文本保存到某处,搜索您当前所在城镇的名称找到了,如果你能找到它,请告诉我.)

但是,当我从我的网络浏览器(即或Firefox)访问" https://www.google.com/#q=where+am+i "时,我会在网页上看到我的城镇名称.在Firefox中,如果我右键单击城镇名称并选择"Inspect Element(Q)",我会清楚地看到用HTML代码编写的城镇名称,这看起来与WebClient返回的原始HTML完全不同.

在我厌倦了玩System.Net.WebBrowser后,我决定给mshtml.HTMLDocument一个镜头,最后得到同样无用的原始HTML:

public static void Main()
{
    mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)new mshtml.HTMLDocument();
    doc.write(new System.Net.WebClient().DownloadString("https://www.google.com/#q=where+am+i"));

    foreach (IHTMLElement e in doc.all)
    {
            System.Diagnostics.Debug.WriteLine(e.outerHTML);
    }
} 
Run Code Online (Sandbox Code Playgroud)

我想必须有一种优雅的方式来获取这种信息.现在,我能想到的是将一个WebBrowser控件添加到表单中,让它导航到相关的URL,发送键"CLRL,A",并将页面上显示的任何内容复制到剪贴板并尝试解析它.不过,这是一个可怕的解决方案.

html javascript c# webbrowser-control dynamic-html

12
推荐指数
2
解决办法
9595
查看次数

是否有一种MVVM友好的方式在WPF中使用WebBrowser控件?

感谢这个问题(点击我!),我的绑定Source属性WebBrowser正确到我的ViewModel.

现在我想实现两个目标:

  1. 获取IsEnabled我的后退和前进按钮的属性以正确绑定到CanGoBack和的CanGoForward属性WebBrowser.
  2. 弄清楚如何在不使用代码隐藏且没有ViewModel必须知道的情况下调用GoForward()GoBack()方法WebBrowser.

我现在有以下(非工作)XAML标记:

<WebBrowser
    x:Name="_instructionsWebBrowser"
    x:FieldModifier="private"
    clwm:WebBrowserUtility.AttachedSource="{Binding InstructionsSource}" />

<Button
    Style="{StaticResource Button_Style}"
    Grid.Column="2"
    IsEnabled="{Binding ElementName=_instructionsWebBrowser, Path=CanGoBack}"
    Command="{Binding GoBackCommand}"
    Content="&lt; Back" />

<Button
    Style="{StaticResource Button_Style}"
    Grid.Column="4"
    IsEnabled="{Binding ElementName=_instructionsWebBrowser, Path=CanGoForward}"
    Command="{Binding GoForwardCommand}"
    Content="Forward &gt;" />
Run Code Online (Sandbox Code Playgroud)

我很确定问题是CanGoBack并且CanGoForward不是依赖属性(并且没有实现INotifyChanged),但我不太确定如何解决这个问题.

问题:

  1. 有什么办法挂钩的附加属性(像我一样用Source)或类似的东西来获取CanGoBackCanGoForward绑定工作?

  2. 怎么写GoBackCommand,GoForwardCommand所以他们独立于代码隐藏和ViewModel,可以在标记中声明?

data-binding wpf xaml webbrowser-control mvvm

11
推荐指数
2
解决办法
7525
查看次数

如何在WebBrowser控件中启用inPrivate模式

我必须在IE浏览器上添加一些额外的功能.

在Visual Studio中,我们有一个名为"WebBrowser"的组件,它使用安装在用户PC中的当前IE浏览器.

但是,我无法找到任何能够访问我希望通过控件公开的InPrivate模式的属性.

有没有办法使用InPrivate模式与WebBrowser控件,或者我是否必须使自己的浏览器支持这个?

c# web-applications webbrowser-control winforms

11
推荐指数
1
解决办法
7917
查看次数