I have a small WPF app (although I guess it doesn't really matter whether it's a wpf form or a webform app?) that I want to have launch a new browser window and POST to a specific url. I've been messing around with:
我有一个小型的WPF应用程序(尽管我想它是WPF表单还是webform应用程序并不重要),我想要启动一个新的浏览器窗口,然后发布到一个特定的url。我一直在胡思乱想:
System.Diagnostics.Process.Start("http://myurl.com");
to launch the window but I don't think I can use the same process to actually post to a url...I've also experimented with HttpWebRequest but I would like the user to be able to use the app after I have posted to this url, not just show them the results...What can I look at to able to do something like this?
打开窗口,但是我不认为我可以用同样的过程来发布到一个url……我也尝试过HttpWebRequest,但是我希望用户可以在我发布到这个url后使用这个应用,而不仅仅是给他们显示结果……我能指望什么来做这样的事呢?
4 个解决方案
#1
3
There are multiple solutions, not sure which one would be the best for you...
有多种解决方案,不确定哪一种最适合你……
- Proceed with your original approach
- 继续您最初的方法
- Embed web browser control in your applicaiton as suggested in other answers
- 在应用程序中嵌入web浏览器控件,如其他答案所示
- Do everything programmatically "behind the scene"
- 以编程的方式“幕后”做所有的事情
For #3 you may want to look here: http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
对于#3,你可能想看看这里:http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
If you want to go with #1 - it is more tricky, since you need to control external application and different browsers would behave differently.
如果您想要使用#1—这就比较麻烦了,因为您需要控制外部应用程序,不同的浏览器的行为会有所不同。
I've used "javascript:" protocol and the code below with IE as default browser when dealing with one "user-unfriendly" application. Please note that it's not "production-ready" code. There is no error handling, user may shift focus away from launched browser, or use browser without "javascript:" protocol support etc.
在处理一个“用户不友好”应用程序时,我使用了“javascript:”协议和下面IE的代码作为默认浏览器。请注意,这不是“生产就绪”代码。没有错误处理,用户可以将焦点从启动的浏览器移开,或者使用没有“javascript:”协议支持的浏览器。
static void Main()
{
Settings s = Settings.Default;
Process.Start(s.URL1);
Thread.Sleep(s.Delay1);
SendKeys.SendWait("%D");
Thread.Sleep(100);
SendKeys.SendWait(EncodeForSendKey(s.URL2));
SendKeys.SendWait("{ENTER}");
}
public static string EncodeForSendKey(string value)
{
StringBuilder sb = new StringBuilder(value);
sb.Replace("{", "{{}");
sb.Replace("}", "{}}");
sb.Replace("{{{}}", "{{}");
sb.Replace("[", "{[}");
sb.Replace("]", "{]}");
sb.Replace("(", "{(}");
sb.Replace(")", "{)}");
sb.Replace("+", "{+}");
sb.Replace("^", "{^}");
sb.Replace("%", "{%}");
sb.Replace("~", "{~}");
return sb.ToString();
}
- URL1: http://www.google.com
- URL1:http://www.google.com
- URL2: javascript:function x(){document.all.q.value='*';document.forms[0].submit();} x();
- URL2:javascript函数x(){ document.all.q.value =“*”;document.forms[0]。submit();} x();
#2
11
There is no direct way to do it. What you could do is generate a HTML page with a form filled with the data you need to post, and a bit of javascript to post the page automatically when it is loaded. Then you just have to open that page in the browser...
没有直接的方法。您可以生成一个HTML页面,其中包含需要发布的数据,以及一些javascript,以便在页面加载时自动发布。然后你只需在浏览器中打开那个页面……
The generated HTML could look like that :
生成的HTML可能是这样的:
<html>
<head>
<script language="Javascript">
function submitForm() {
var theForm = document.getElementById("theForm");
theForm.submit();
}
</script>
</head>
<body onload="submitForm()">
<form id="theForm" action="http://myurl.com" method="POST">
<input type="text" name="username" value="myusername"/>
<input type="password" name="password" value="mypassword"/>
</form>
</body>
</html>
If the page must be displayed in your application, load it in a WebBrowser
control
如果页面必须显示在应用程序中,请在WebBrowser控件中加载它
#4
3
You can create a hidden WebBrowser
control and do Navigate()
(using the overload that allows you to specify the request method). You will need to specify a "_blank" target frame to cause the navigation to happen in a new browser window.
您可以创建一个隐藏的WebBrowser控件并执行navigation()(使用允许您指定请求方法的重载)。您将需要指定一个“_blank”目标帧,以使导航在新的浏览器窗口中发生。
#1
3
There are multiple solutions, not sure which one would be the best for you...
有多种解决方案,不确定哪一种最适合你……
- Proceed with your original approach
- 继续您最初的方法
- Embed web browser control in your applicaiton as suggested in other answers
- 在应用程序中嵌入web浏览器控件,如其他答案所示
- Do everything programmatically "behind the scene"
- 以编程的方式“幕后”做所有的事情
For #3 you may want to look here: http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
对于#3,你可能想看看这里:http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
If you want to go with #1 - it is more tricky, since you need to control external application and different browsers would behave differently.
如果您想要使用#1—这就比较麻烦了,因为您需要控制外部应用程序,不同的浏览器的行为会有所不同。
I've used "javascript:" protocol and the code below with IE as default browser when dealing with one "user-unfriendly" application. Please note that it's not "production-ready" code. There is no error handling, user may shift focus away from launched browser, or use browser without "javascript:" protocol support etc.
在处理一个“用户不友好”应用程序时,我使用了“javascript:”协议和下面IE的代码作为默认浏览器。请注意,这不是“生产就绪”代码。没有错误处理,用户可以将焦点从启动的浏览器移开,或者使用没有“javascript:”协议支持的浏览器。
static void Main()
{
Settings s = Settings.Default;
Process.Start(s.URL1);
Thread.Sleep(s.Delay1);
SendKeys.SendWait("%D");
Thread.Sleep(100);
SendKeys.SendWait(EncodeForSendKey(s.URL2));
SendKeys.SendWait("{ENTER}");
}
public static string EncodeForSendKey(string value)
{
StringBuilder sb = new StringBuilder(value);
sb.Replace("{", "{{}");
sb.Replace("}", "{}}");
sb.Replace("{{{}}", "{{}");
sb.Replace("[", "{[}");
sb.Replace("]", "{]}");
sb.Replace("(", "{(}");
sb.Replace(")", "{)}");
sb.Replace("+", "{+}");
sb.Replace("^", "{^}");
sb.Replace("%", "{%}");
sb.Replace("~", "{~}");
return sb.ToString();
}
- URL1: http://www.google.com
- URL1:http://www.google.com
- URL2: javascript:function x(){document.all.q.value='*';document.forms[0].submit();} x();
- URL2:javascript函数x(){ document.all.q.value =“*”;document.forms[0]。submit();} x();
#2
11
There is no direct way to do it. What you could do is generate a HTML page with a form filled with the data you need to post, and a bit of javascript to post the page automatically when it is loaded. Then you just have to open that page in the browser...
没有直接的方法。您可以生成一个HTML页面,其中包含需要发布的数据,以及一些javascript,以便在页面加载时自动发布。然后你只需在浏览器中打开那个页面……
The generated HTML could look like that :
生成的HTML可能是这样的:
<html>
<head>
<script language="Javascript">
function submitForm() {
var theForm = document.getElementById("theForm");
theForm.submit();
}
</script>
</head>
<body onload="submitForm()">
<form id="theForm" action="http://myurl.com" method="POST">
<input type="text" name="username" value="myusername"/>
<input type="password" name="password" value="mypassword"/>
</form>
</body>
</html>
If the page must be displayed in your application, load it in a WebBrowser
control
如果页面必须显示在应用程序中,请在WebBrowser控件中加载它
#3
#4
3
You can create a hidden WebBrowser
control and do Navigate()
(using the overload that allows you to specify the request method). You will need to specify a "_blank" target frame to cause the navigation to happen in a new browser window.
您可以创建一个隐藏的WebBrowser控件并执行navigation()(使用允许您指定请求方法的重载)。您将需要指定一个“_blank”目标帧,以使导航在新的浏览器窗口中发生。