正如标题所说......当用户点击Java Swing应用程序中的按钮时,我尝试使用以下代码执行PHP脚本:
URL url = new URL( "http://www.mywebsite.com/my_script.php" );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
但没有任何反应...... 有什么问题吗?
答案 0 :(得分:5)
我认为你错过了下一步,例如:
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
}
答案 1 :(得分:1)
final URL url = new URL("http://domain.com/script.php");
final InputStream inputStream = new InputStreamReader(url);
final BufferedReader reader = new BufferedReader(inputStream).openStream();
String line, response = "";
while ((line = reader.readLine()) != null)
{
response = response + "\r" + line;
}
reader.close();
“回复”将保留页面文本。您可能想要回车(取决于操作系统,尝试\ n,\ r或两者的组合)。
希望这有帮助。