简体   繁体   English

C#:从Windows桌面应用程序打开浏览器并POST到URL

[英]C#: Open a browser and POST to a url from a windows desktop app

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. 我有一个小型的WPF应用程序(虽然我认为无论是wpf表单还是webform应用程序并不重要?)我想要启动一个新的浏览器窗口并POST到特定的URL。 I've been messing around with: 我一直在搞乱:

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? 启动窗口,但我不认为我可以使用相同的过程实际发布到网址...我也尝试过HttpWebRequest但我希望用户能够在发布到我之后使用该应用程序这个网址,不只是向他们展示结果......我能看到什么才能做到这样的事情?

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. 您可以做的是生成一个HTML页面,其中的表单中填充了您需要发布的数据,以及一些javascript,以便在加载时自动发布页面。 Then you just have to open that page in the browser... 然后你只需要在浏览器中打开该页面......

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控件中

请改用WebBrowser类

You can create a hidden WebBrowser control and do Navigate() (using the overload that allows you to specify the request method). 您可以创建隐藏的WebBrowser控件并执行Navigate() (使用允许您指定请求方法的重载)。 You will need to specify a "_blank" target frame to cause the navigation to happen in a new browser window. 您需要指定一个“_blank”目标框架,以便在新的浏览器窗口中进行导航。

There are multiple solutions, not sure which one would be the best for you... 有多种解决方案,不确定哪一种最适合您......

  1. Proceed with your original approach 继续您的原始方法
  2. Embed web browser control in your applicaiton as suggested in other answers 按照其他答案中的建议,在您的应用程序中嵌入Web浏览器控件
  3. 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. 在处理一个“用户不友好”的应用程序时,我使用了“javascript:”协议和IE下面的代码作为默认浏览器。 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:”协议支持等的浏览器。

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='stackoverflow';document.forms[0].submit();} x(); URL2:javascript:function x(){document.all.q.value ='stackoverflow'; document.forms [0] .submit();} x();

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

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