PHP测试Java类(关于将数据发布到web)
我编写了一个程序将字符串发送到Web URL,但在发送之前,我需要在localhost上使用PHP对其进行测试。我从未学过PHP。你们能帮我吗PHP测试Java类(关于将数据发布到web),java,php,Java,Php,我编写了一个程序将字符串发送到Web URL,但在发送之前,我需要在localhost上使用PHP对其进行测试。我从未学过PHP。你们能帮我吗 public class MessageSender { public static Boolean sendMsg(String outboundUrl,String message) { Boolean sendResult = false; URL url; HttpURLConnection
public class MessageSender {
public static Boolean sendMsg(String outboundUrl,String message) {
Boolean sendResult = false;
URL url;
HttpURLConnection connection = null;
try {
message = URLEncoder.encode("message", "UTF-8");
//Create connection
url = new URL(outboundUrl);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(message.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (message);
wr.flush ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
wr.close ();
rd.close();
String Whole=response.toString();
//System.out.println(Whole);
sendResult = true;
}
catch (Exception e) {
e.printStackTrace();
return sendResult;
}
finally {
if(connection != null) {
connection.disconnect();
}
}
return sendResult;
}
}
我不知道如何在本地主机上测试使用PHP如果您已经安装了PHP,请创建一些文件夹ie/tmp/phppublic并在那里放置index.PHP文件 index.php
<?php
var_dump($_POST);
?>
测试Java集的步骤
outboundUrl=”http://localhost:8000/index.php“
那么,您需要知道您自己的本地主机是否可以使用php?还是说外部主机?为什么你们需要知道呢?我试过我可以在eclipse中打印,但在浏览器上是shows Array();将var_dump用作$_postis数组,这样就可以很好地转储数据,也可以将数据转储到Java ucomment//System.out.println(Whole),您不能直接从浏览器中访问它$\u POST变量将为空,因为您没有发送任何内容,使用Java您实际上是在发送POST请求,所以只需看看eclipsethanks中会显示什么!但是如果我想在我的浏览器上看到它,我该怎么办呢?你可以使用$_GET['someparamname';所以点击http://localhost:8000/index.php?message=somemessagehere
和更改phpecho$\u GET['message']代码>如果你能解释你到底需要测试什么,这也会很有帮助。如果您需要一篇文章来工作,您需要在index.php中有一个HTML部分,它显示一个包含消息输入元素的帖子表单,并将其提交到同一个index.php,我希望您知道HMTL;)var消息是XML的内容。我想把它贴在URL上。谢谢你,伙计!!!
cd /tmp/phppublic
php -S localhost:8000