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 ?

1
  • 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. Commented Oct 26, 2010 at 12:27

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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
Hi, I want to ask about InputStream. Why to run php script we need InputStream not the OutputStream?
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.