从java运行php文件?

时间:2013-03-22 22:07:14

标签: java php web

我有以下代码。

    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();

由于某种原因,它没有在网站上运行PHP脚本,我不知道什么是错的,请帮忙!

我知道脚本运行正常,如果我把它放在我的webbrowser中它插入信息很好,但它不是用Java。

1 个答案:

答案 0 :(得分:1)

openConnection()实际上并没有加载页面。你需要获得从那里返回的UrlConnection对象,然后是connect()getContent()(也许你不需要getContent(),先试试吧)

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();

此处有更多信息:http://docs.oracle.com/javase/6/docs/api/java/net/URLConnection.html