如何从Java执行PHP脚本? [英] How can I execute a PHP script from Java?
问题描述
我有一个通过URL执行的php脚本。
(例如www.something.com/myscript?param=xy)
I have a php script which is executed over a URL. (e.g. www.something.com/myscript?param=xy)
当在浏览器中执行此脚本时,它会提供编码结果,为负数或正数。
When this script is executed in a browser it gives a coded result, a negative or positive number.
我想从Java代码(J2EE)执行此脚本并将结果存储在某个对象中。
I want to execute this script from Java code(J2EE) and store that result in some object.
我正在尝试使用 httpURLConnection
。
我建立连接但无法获取结果。我不确定我是否执行该脚本。
I'm trying to use httpURLConnection
for that.
I establish a connection but can not fetch the result. I'm not sure if I execute the script at all.
推荐答案
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
此片段来自官方Java教程( http://java.sun.com/docs/books/tutorial/networking /urls/readingWriting.html )。这应该对你有帮助。
This snippet is from the offical Java tutorial (http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html). This should help you.
这篇关于如何从Java执行PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!