简体   繁体   English

使用Silverlight和JavaScript在浏览器中进行HTTP发布参数和显示响应

[英]Http-post parameters and display response in a browser using Silverlight and JavaScript

While doing Silverlight's interoperability with JavaScript I've faced a strange behaviour in my Silverlight Out-of-Browser (OOB) application. 在实现Silverlight与JavaScript的互操作性时,我在Silverlight浏览器外(OOB)应用程序中遇到了一个奇怪的行为。 My goal is to http-post some data to the specific site. 我的目标是将一些数据http发布到特定站点。 We can post data using WebClient or HTTPWebRequest classes. 我们可以使用WebClient或HTTPWebRequest类发布数据。 But then how to redirect the response to external web browser? 但是,如何将响应重定向到外部Web浏览器? I thought I would generate a dynamic HTML form with post method and invoke a JavaScript function from Silverlight which would submit the form. 我以为我会用post方法生成一个动态HTML表单,并从Silverlight调用一个JavaScript函数来提交表单。 Unfortunately it only submits the form with target="_self" and not with target="_blank". 不幸的是,它仅使用target =“ _ self”而不是target =“ _ blank”提交表单。 Code for loading generated html string into Silverlight's WebBrowser control: 用于将生成的html字符串加载到Silverlight的WebBrowser控件中的代码:

WebBrowser wb = parameter as WebBrowser;
if (wb == null)
    return;

wb.NavigateToString(GetHtml());

Simple test method to generate HTML: 生成HTML的简单测试方法:

private string GetHtml ()
{
    StringBuilder sb = new StringBuilder ();
    sb.Append ("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
    sb.Append ("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
    sb.Append ("<head><title></title>");
    sb.Append(
        "<script type=\"text/javascript\">function submitForm(){" +
        "document.forms[0].submit(); window.external.notify(\"Form Submitted!\");}</script>");
    sb.Append("</head>");
    sb.Append ("<body>");
    sb.Append ("<form id=\"mainForm\" name=\"formName1\" action=\"http://localhost:53222/ReceivePost.aspx\" method=\"POST\" target=\"_blank\">");
    sb.Append ("<input type=\"text\" name=\"u\" value=\"username\"/>");
    sb.Append ("<input type=\"password\" name=\"p\" value=\"password\"/>");
    sb.Append ("<input type=\"submit\" value=\"Submit\"/>");
    sb.Append("</form>");
    sb.Append("</body></html>");
    return sb.ToString ();
}

which generates such HTML page: 生成这样的HTML页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
            function submitForm(){
                document.forms[0].submit();
                window.external.notify("Form Submitted!");
            }
        </script>
    </head>
    <body>
        <form id="mainForm" name="formName1" action="http://localhost:53222/ReceivePost.aspx" method="POST" target="_blank">
            <input type="text" name="u" value="username"/>
            <input type="password" name="p" value="password"/>
            <input type="submit" value="Submit"/>
        </form>
    </body>
</html>

And the code for invoking JS function: 以及调用JS函数的代码:

innerWebBrowser.ScriptNotify += (o, args) =>
    {
        MessageBox.Show("JS says: " + args.Value);
    };
innerWebBrowser.InvokeScript ("submitForm");

As I said, this code works fine with target set to "_self" but doesn't work with target set to "_blank". 如我所说,此代码在将目标设置为“ _self”的情况下可以正常工作,但在将目标设置为“ _blank”的情况下则无法工作。 Even if I press Submit button with a mouse the post data is not sent, just web site is rendered in external browser without post data. 即使我用鼠标按下Submit按钮,也不会发送帖子数据,只是网站在外部浏览器中呈现而没有帖子数据。 Is it due to the security mechanism in Silverlight? 是否由于Silverlight中的安全性机制? I should also mention that I am posting to the same site where Silverlight app is published: http://localhost:53222 (well it's not published yet as I run it straight from VisualStudio in OOB mode). 我还应该提到,我要发布到发布Silverlight应用程序的站点: http://localhost:53222 (而且还没有发布,因为我是直接在http://localhost:53222以OOB模式运行它的)。 Thus it shouldn't be categorized as cross-site scripting I guess... 因此,我猜它不应该被归类为跨站点脚本。

Is there any way to post data to some website (not necessarily from the same domain) and redirect the response to external browser? 是否可以将数据发布到某个网站(不一定来自同一域)并将响应重定向到外部浏览器?

Anyway, your comments are most welcome. 无论如何,最欢迎您发表评论。

I've come up with a workaround which performs exactly what I wanted (starts external browser and loads specific web site while posting some parameters to it). 我想出一种解决方法,可以完全满足我的要求(启动外部浏览器并加载特定网站,同时向其中发布一些参数)。 Instead of using WebBrowser control HTML file (similar as in the question) with Form and a JavaScript to submit the form onLoad event is generated. 而不是使用带有Form和JavaScript的WebBrowser控件HTML文件(类似于问题中的代码)来生成表单onLoad事件。 Then the generated HTML content is written into the file system and started with default browser later on. 然后,将生成的HTML内容写入文件系统,并稍后使用默认浏览器启动。

File may be created with similar code: 可以使用类似的代码创建文件:

using (StreamWriter file = File.CreateText (filePath))
{
    file.WriteLine (GetHtml());
    file.Close ();
}

Default browser may be opened with something like the following code: 可能会使用以下代码打开默认浏览器:

using (dynamic shell = AutomationFactory.CreateObject ("WScript.Shell"))
{
    shell.Run (filePath, 7, false);
}

And after the default browser is up and running we may delete the file we just generated: 在默认浏览器启动并运行后,我们可以删除刚刚生成的文件:

if (File.Exists (filePath))
{
    File.Delete (filePath);
}

Please note that in order to create/delete a file your SL application needs to be trusted application. 请注意,为了创建/删除文件,您的SL应用程序必须是受信任的应用程序。 That is, it has to have elevated permissions (check Application.Current.HasElevatedPermissions property). 也就是说,它必须具有提升的权限(检查Application.Current.HasElevatedPermissions属性)。

Access local file system in Silverlight 5 . 在Silverlight 5中访问本地文件系统 In SL 4 one could use com interoperability and Scripting.FileSystemObject to access local file system. 在SL 4中,可以使用com互操作性和Scripting.FileSystemObject访问本地文件系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM