正如标题所说...当用户单击我的 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
}
关于java - 我如何从 Java 代码调用 PHP 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4023533/