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

Bra*_*rad 3 php java execute

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

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

但没有任何反应...... 有什么不对吗?

Gar*_*vis 5

我想你错过了下一步,例如:

InputStream is = conn.getInputStream();
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)