C#中:从Windows桌面应用程序,打开浏览器并张贴到网址 [英] C#: Open a browser and POST to a url from a windows desktop app

查看:158
本文介绍了C#中:从Windows桌面应用程序,打开浏览器并张贴到网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小WPF应用程序(虽然我想这并不重要,它是否是一个WPF形式或一个WebForm的应用程序?),我想有推出新的浏览器窗口,并张贴到特定的URL。我一直在瞎搞用:

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:

System.Diagnostics.Process.Start("http://myurl.com");

启动窗口,但我不认为我可以使用相同的过程实际上张贴到网址...我也尝试了HttpWebRequest的,但我希望用户能够在我使用的应用程序已发布到这个网址,而不仅仅是展示他们的结果...我可以看看在哪些要能够做这样的事?

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?

推荐答案

有多个解决方案,不知道哪一个是最适合你的...

There are multiple solutions, not sure which one would be the best for you...


  1. 与你原来的做法继续

  2. 在其他的答案建议你的应用程序了嵌入Web浏览器控件

  3. 请一切以编程幕后操纵者

有关#3你可能想在这里看看:的http:// geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

For #3 you may want to look here: http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

如果你想要去的#1 - 这是更靠谱,因为你需要控制外部应用和不同的浏览器会表现不同。

If you want to go with #1 - it is more tricky, since you need to control external application and different browsers would behave differently.

我用的JavaScript。请注意,这不是生产就绪code。没有错误处理,用户可以不用的javascript:转移焦点从浏览器启动,或使用的浏览器了。协议支持等。

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.

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

  • URL2:JavaScript的:函数x(){document.all.q.value ='计算器'; document.forms [0] .submit();} X();

  • 这篇关于C#中:从Windows桌面应用程序,打开浏览器并张贴到网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆