简体   繁体   English

使用C#使用默认浏览器发送HTTP Post

[英]Send HTTP Post with default browser with C#

I am wondering if it is possible to send POST data with the default browser of a computer in C#. 我想知道是否可以使用C#中的计算机的默认浏览器发送POST数据。

Here is the situation. 情况就是这样。 My client would like the ability to have their C# application open their browser and send client information to a webform. 我的客户希望能够让他们的C#应用​​程序打开他们的浏览器并将客户端信息发送到webform。 This webform would be behind a login screen. 此网络表单将位于登录屏幕后面。 The assumption from the application side is that once the client data is sent to the login screen, the login screen would pass that information onto the webform to prepopulate it. 应用程序方面的假设是,一旦客户端数据被发送到登录屏幕,登录屏幕就会将该信息传递到webform上以预先填充它。 This would be done over HTTPS and the client would like this to be done with a POST and not a GET as client information would be sent as plain text. 这将通过HTTPS完成,客户端希望通过POST而不是GET完成此操作,因为客户端信息将以纯文本形式发送。

I have found some wonderful solutions that do POSTS and handle the requests. 我找到了一些做POSTS并处理请求的精彩解决方案。 As an example http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx 例如http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

So the TL;DR version of this would be 所以TL; DR版本就是这样

1) Open Browser 1)打开浏览器

2) Open some URL with POST data 2)用POST数据打开一些URL

Thanks for your help, 谢谢你的帮助,

Paul 保罗

I've handled a similar situation once by generating an HTML page on the fly with a form setup with hidden values for everything. 我曾经通过动态生成一个HTML页面来处理类似的情况,表单设置中包含所有内容的隐藏值。 There was a bit of Javascript on the page so that when it loaded, it would submit the form, therefore posting the data as necessary. 页面上有一些Javascript,因此当它加载时,它将提交表单,因此必要时发布数据。

I suspect this method would work for you. 我怀疑这种方法对你有用。

  1. Generate a dictionary of fields and values 生成字段和值的字典
  2. Generate an HTML page with the Javascript to automatically submit when page is loaded 使用Javascript生成HTML页面,以便在加载页面时自动提交
  3. Write page to a temp location on disk 将页面写入磁盘上的临时位置
  4. Launch default browser with that page 使用该页面启动默认浏览器

Remember though that POST data is sent plaintext as well. 请记住,POST数据也是以明文形式发送的。 POST is generally the way to go for more than a couple fields, as you can fit in more data (2048 byte limit on URLs) and that your user has a friendly URL to see in their browser. POST通常是指向多个字段的方式,因为您可以容纳更多数据(URL上的2048字节限制),并且您的用户有一个友好的URL可以在浏览器中查看。

Nothing is sent as plain text when you use SSL, it is encrypted. 使用SSL时,没有任何内容以纯文本形式发送,它是加密的。 Unless you set what the default browser is (IE, Firefox, Chrome, etc), then you'll have to figure out what the default browser is and use its API to do this work (if it's possible). 除非你设置默认浏览器(IE,Firefox,Chrome等),否则你必须弄清楚默认浏览器是什么,并使用它的API来完成这项工作(如果可能的话)。

What would probably be must faster and more efficient would be to open the default browser by invoking a URL with Start Process and pass the information on the query string (this is doing a GET instead of a POST, which I know isn't what you're asking for). 可能必须更快,更有效的是通过调用带有Start Process的URL打开默认浏览器并传递查询字符串上的信息(这是做一个GET而不是POST,我知道这不是你的'要求)。

The response from the server could be a redirect, and the redirect could send down the filled-out form (storing the values in session or something similar). 来自服务器的响应可以是重定向,并且重定向可以向下发送填写的表单(在会话中存储值或类似的东西)。

That way the complexity is pushed to the website and not the windows application, which should be easier to update if something goes wrong. 这样,复杂性被推送到网站而不是Windows应用程序,如果出现问题,应该更容易更新。

HTH HTH

Can you compile your logic in C# and then call it from PowerShell? 你能用C#编译逻辑然后从PowerShell调用吗? From PowerShell you can very easily automate Internet Explorer . 从PowerShell,您可以非常轻松地自动化Internet Explorer This is IE only but you might be able to also use WaitnN . 这只是IE,但您也可以使用WaitnN

Anything you put at the end of the URL counts as the querystring, which is what GET fills. 你放在URL末尾的任何内容都算作查询字符串,这就是GET填充的内容。 It is more visible than the POSTed data in the body, but no more secure with regard to a sniffer. 它比身体中的POSTed数据更明显,但对于嗅探器来说不再安全。

So, in short, no. 所以,简而言之,没有。

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

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