[英]Sending data directly from Browser to Java Desktop application
I'm making a web application using TypeScript
as the client-side scripting language. 我正在使用
TypeScript
作为客户端脚本语言来制作Web应用程序。 Further technologies I use are Spring Boot
, PostgreSQL
and Angular
. 我使用的其他技术是
Spring Boot
, PostgreSQL
和Angular
。 When the user clicks a button on the website, my already running Java
desktop application ("independent" from the web application, not the web application's server) should receive data sent from the web application and then prompt the user to import it into the Desktop application. 当用户单击网站上的按钮时,我已经运行的
Java
桌面应用程序(与Web应用程序“独立”,而不是Web应用程序的服务器的“独立”)应接收从Web应用程序发送的数据,然后提示用户将其导入到Desktop中应用。
How can this be done? 如何才能做到这一点? Any example codes available?
有可用的示例代码吗?
Can I start a simple listener server on my Desktop application waiting for the client (the browser) to send the payload of import data to the listening server (in a platform-independent way)? 是否可以在桌面应用程序上启动一个简单的侦听器服务器,以等待客户端(浏览器)将导入数据的有效负载发送到侦听服务器(以与平台无关的方式)?
Note that this is different from the Desktop application to download data from an Internet resource since the resource should be determined by a click in the browser itself , not through the Desktop application's interface. 请注意,这不同于从Internet资源下载数据的桌面应用程序,因为该资源应由浏览器本身中的单击确定 ,而不是通过桌面应用程序的界面来确定。
Registering my Java
Desktop application to an URI
scheme only allows me to launch the Desktop application with a custom URI
as parameter and it's Windows
only as well which is therefore not a satisfying solution. 将我的
Java
Desktop应用程序注册到URI
方案仅允许我以自定义URI
作为参数启动Desktop应用程序,并且它也仅是Windows
,因此也不是令人满意的解决方案。
I understand your setup as follows: 我了解您的设置如下:
Browser <--HTTP/HTTPS via Internet-->SpringBoot 浏览器<-通过Internet的HTTP / HTTPS-> SpringBoot
A webpage rendered in the browser should communicate with a local desktop application. 浏览器中呈现的网页应与本地桌面应用程序通信。
The solution is to establish a HTTP connection between your webpage and the desktop application as follows: 解决方案是按照以下步骤在网页和桌面应用程序之间建立HTTP连接:
var req = new XMLHTTPRequest(); req.open("POST", " http://127.0.0.1:yourPort "); req.setRequestHeader("content-type", "application/x-www-form-urlencoded"); req.send("payload="+ myPayload);
When the browser has established a HTTPS connection with the SpringBoot server, you have the problem that the browser may display a "Mixed Content Warning" as a secured web context (your web page) accesses a resource via HTTP. 当浏览器与SpringBoot服务器建立HTTPS连接时,由于安全的Web上下文(您的网页)通过HTTP访问资源,浏览器可能会显示“混合内容警告”。
Fortunately, lately modern browsers like Chrome, Firefox, Edge and Safari implement the W3C Secure Contexts working draft and thus consider access to 127.0.0.1 as potentially trustworthy: no Mixed Content warning. 幸运的是,最近的现代浏览器(例如Chrome,Firefox,Edge和Safari)实现了W3C Secure Contexts工作草案 ,因此将对127.0.0.1的访问视为潜在可信任:没有混合内容警告。
Still if you bring this into production you must be aware that your desktop application is now vulnerable against XSS attacks. 但是,如果将其投入生产,则必须知道您的桌面应用程序现在容易受到XSS攻击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.