正如标题所示……当用户单击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基本上只打开连接上的套接字,以便做一些你需要做的事情,比如调用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
}