3

As the title says ... I have tried to use the following code to execute a PHP script when user clicks a button in my Java Swing application :

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

But nothing happens ... Is there something wrong ?

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Brad
  • 4,457
  • 10
  • 56
  • 93
  • This isn't exactly "calling PHP from Java". It's just "making an HTTP request". The other end of that HTTP request might be PHP... or it might be static content, Ruby on Rails, or yet more Java. From your code's perspective, it's simply an HTTP request. So Gareth Davis is correct below... once you open that HTTP connection, you have to invoke another method to send your HTTP request and get an HTTP response. This is true even you don't care about doing anything with that response. – Steve Perkins Oct 26 '10 at 12:27

2 Answers2

6

I think you're missing the next step which is something like:

InputStream is = conn.getInputStream();

HttpURLConnection basically only opens the socket on connect in order to do something you need to do something like calling getInputStream() or better still 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
}
Cyrille
  • 3,427
  • 1
  • 30
  • 32
Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
  • Thanks Gareth ... It now works fine. I first thought that connect() will automatically call the url and execute it directly since i'm using HttpURLConnection and not URLConnection. – Brad Oct 26 '10 at 12:33
  • No worries. As you can see from my second example, calls to connect() are actually optional. to be honest if you are doing anything other than the simplest thing you might want to consider using apache's HttpClient – Gareth Davis Oct 26 '10 at 12:35
  • Hi, I want to ask about InputStream. Why to run php script we need InputStream not the OutputStream? – aLIEz Mar 24 '16 at 11:36
  • Because you reading bytes into the java program, ie InputStream is reading data output stream is sending (or writting) data out of the java program. Hope that helps. – Gareth Davis Mar 25 '16 at 08:17
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();

"response" will hold the text of the page. You may want to play around with the carriage return (depending on the OS, try \n, \r, or a combination of both).

Hope this helps.

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144