Http-post parameters and display response in a browser using Silverlight and JavaScript -
while doing silverlight's interoperability javascript i've faced strange behaviour in silverlight out-of-browser (oob) application. goal http-post data specific site. can post data using webclient or httpwebrequest classes. how redirect response external web browser? thought generate dynamic html form post method , invoke javascript function silverlight submit form. unfortunately submits form target="_self" , not target="_blank". code loading generated html string silverlight's webbrowser control:
webbrowser wb = parameter webbrowser; if (wb == null) return; wb.navigatetostring(gethtml());
simple test method generate 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:
<!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 code invoking js function:
innerwebbrowser.scriptnotify += (o, args) => { messagebox.show("js says: " + args.value); }; innerwebbrowser.invokescript ("submitform");
as said, code works fine target set "_self" doesn't work target set "_blank". if press submit button mouse post data not sent, web site rendered in external browser without post data. due security mechanism in silverlight? should mention posting same site silverlight app published: http://localhost:53222
(well it's not published yet run straight visualstudio in oob mode). shouldn't categorized cross-site scripting guess...
is there way post data website (not same domain) , redirect response external browser?
anyway, comments welcome.
i've come workaround performs wanted (starts external browser , loads specific web site while posting parameters it). instead of using webbrowser
control html file (similar in question) form , javascript submit form onload
event generated. generated html content written file system , started default browser later on.
file may created similar code:
using (streamwriter file = file.createtext (filepath)) { file.writeline (gethtml()); file.close (); }
default browser may opened following code:
using (dynamic shell = automationfactory.createobject ("wscript.shell")) { shell.run (filepath, 7, false); }
and after default browser , running may delete file generated:
if (file.exists (filepath)) { file.delete (filepath); }
please note in order create/delete file sl application needs trusted application. is, has have elevated permissions (check application.current.haselevatedpermissions
property).
access local file system in silverlight 5. in sl 4 1 use com interoperability , scripting.filesystemobject
access local file system.
Comments
Post a Comment