如何从 Java 代码调用 PHP 脚本?

2024-03-30

正如标题所说...当用户单击 Java Swing 应用程序中的按钮时,我尝试使用以下代码执行 PHP 脚本:

URL url = new URL( "http://www.mywebsite.com/my_script.php" );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();

但什么也没发生...有什么不对 ?


我认为您错过了下一步,如下所示:

InputStream is = conn.getInputStream();

HttpURLConnection基本上只打开套接字connect为了做某事,你需要做一些事情,比如打电话getInputStream()或者更好getResponseCode()

URL url = new URL( "http://google.com/" );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
    InputStream is = conn.getInputStream();
    // do something with the data here
}else{
    InputStream err = conn.getErrorStream();
    // err may have useful information.. but could be null see javadocs for more information
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Java 代码调用 PHP 脚本? 的相关文章

随机推荐