1

通常当我得到 POST 数据时,它是从 HTML 表单发送的,并且参数有名称,即 from <input type="text" name="yourname" />,然后我可以使用 php 接收和打印这些数据echo $_POST['yourname'];

现在我正在实现一个 Java 应用程序,它应该将 XML 格式的数据 POST 到指定的 URL。我写了一个简单的 PHP 页面,我可以尝试向其发布数据,但没有打印数据。而且由于没有任何参数的名称,我不知道应该如何用 PHP 打印它。

我尝试将简单的 XML 发送到服务器,服务器应该响应MESSAGE: <XML>,但它只响应MESSAGE:. 我究竟做错了什么?

这是我在服务器上的 PHP 代码:

<?php 
echo 'MESSAGE:';
print_r($_POST); ?>

这是我在客户端上的 Java 代码:

String xmlRequestStatus = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
    <test></test>";
String contentType = "text/xml";
String charset = "ISO-8859-1";
String request = null;
try {
    request = String.format("%s",
        URLEncoder.encode(xmlRequestStatus, charset));
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
URL url = null;
URLConnection connection = null;
OutputStream output = null;
InputStream response = null;
try {
    url = new URL("http://skogsfrisk.se/test/");
} catch (MalformedURLException e) {
    e.printStackTrace();
}

try {
    connection = url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Accept-Charset", charset);
    connection.setRequestProperty("Content-Type", contentType);
    output = connection.getOutputStream();
    output.write(request.getBytes("ISO-8859-1"));
    if(output != null) try { output.close(); } catch (IOException e) {}

    response = connection.getInputStream();
    ...
} catch (IOException e) {
    e.printStackTrace();
}
4

2 回答 2

3

不,$_POST 数组仅使用multipart/form-dataenctype 填充。

您可以将 fopen() 与 php://input 包装器或 $HTTP_RAW_POST_DATA
http://www.php.net/manual/en/wrappers.php.php一起使用

于 2010-06-29T18:03:01.880 回答
0

发布 xml 后尝试在浏览器中查看页面的源代码,如果您正常查看页面,则 xml 不会显示,除非您将其传递给 htmlspecialchars()。print_r($_POST);是xss所以要小心。

于 2010-06-29T18:06:21.837 回答