简体   繁体   English

通过Java将HTML代码传递给浏览器

[英]Passing HTML code to browser via java

I'm working on a desktop application using Swing. 我正在使用Swing开发桌面应用程序。 I have figured out how to log into a website using a POST request and receiving the HTML response. 我已经弄清楚了如何使用POST请求登录网站并接收HTML响应。 Now I need to send the received HTML to the default browser. 现在,我需要将接收到的HTML发送到默认浏览器。 Basically, displaying to the user what the browser is returning to me in the background. 基本上,在后台向用户显示浏览器返回给我的是什么。 I have read a bit about set header, etc. 我已经阅读了一些有关设置标头的信息,等等。

So my question is, how can I post html code from java into a browser in such a manner that it displays as a website and not just text? 所以我的问题是,如何才能将java中的html代码发布到浏览器中,使其不仅显示为网站,还显示为网站?

I would love not to have to use any server (Socket) type code because I feel there is a compatibility risk if the program is run from different computers. 我希望不必使用任何服务器(套接字)类型的代码,因为如果从不同的计算机运行该程序,则存在兼容性风险。

    // Log Into Website

    URL objtest = new URL("http://rccpdems01/ems/Login.php/Login.php");
    HttpURLConnection con = (HttpURLConnection) objtest.openConnection();
    con.setRequestMethod("POST"); 
    String datas = "userid=Gt737326&psword=Test&btnLogin=Login";
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(datas);
    wr.flush();
    wr.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();

If it is a desktop program,I suppose you want to do this 如果它是桌面程序,我想您要这样做

Runtime runtime=RunTime.getRuntime();
 String path="www link or a local html file";
 String browserpath="path to your browser like c:/Program Files/firefox/firefox.exe";
 Process process=runtime.exec(browserpath + path);
 process.waitFor();

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

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