Richard Collins
Richard Collins

Reputation: 1

Run php file on site from java?

I have the following code.

    String name = "hello";
    String pass = "testing";
    String des = "this is info";

    String u = "http://mysite.com/insertinfo.php?name=" + name + "&pass=" + pass + "&description=" + des;

    URL url = new URL(u);
    url.openConnection();

for some reason it isn't running the php script on the site and I don't know what's wrong, please help!

I know that the script runs properly, if I put it in my webbrowser it inserts info fine, but it doesn't in Java.

Upvotes: 0

Views: 50

Answers (1)

Populus
Populus

Reputation: 7680

openConnection() doesn't actually load the page. You need to get the UrlConnection object that is returned from that, then connect() and getContent() (maybe you don't need the getContent(), try it without first)

e.g.

String name = "hello";
String pass = "testing";
String des = "this is info";

String u = "http://mysite.com/insertinfo.php?name=" + name + "&pass=" + pass + "&description=" + des;

URL url = new URL(u);
UrlConnection conn = url.openConnection();
conn.connect();
conn.getContent();

more info here: http://docs.oracle.com/javase/6/docs/api/java/net/URLConnection.html

Upvotes: 1

Related Questions