[英]How can I send XML from a Java application and print it with PHP on the server, using HTTP POST?
Usually when I get POST data it's send from a HTML form and the parameters has names, ie from <input type="text" name="yourname" />
, then I can receive and print this data with php echo $_POST['yourname'];
通常,当我获取POST数据时,它是从HTML表单发送的,并且参数具有名称,即来自<input type="text" name="yourname" />
,然后我可以使用php echo $_POST['yourname'];
接收并打印此数据echo $_POST['yourname'];
. 。
Now I am implementing a Java application that should POST data in XML format to a specified URL. 现在,我正在实现一个Java应用程序,该应用程序应该将XML格式的数据发布到指定的URL。 I wrote a simple PHP page that I can try to POST data to, but the data is not printed. 我编写了一个简单的PHP页面,可以尝试将数据发布到该页面,但是不会打印数据。 And since there is no name of any parameters I don't know how I should print it with PHP. 而且由于没有任何参数的名称,所以我不知道如何使用PHP打印它。
I have tried to send simple XML to the server and the server should respond with MESSAGE: <XML>
, but it only response with MESSAGE:
. 我尝试将简单的XML发送到服务器,并且服务器应使用MESSAGE: <XML>
响应,但它仅使用MESSAGE:
响应。 What am I doing wrong? 我究竟做错了什么?
Here is my PHP-code on the server: 这是我在服务器上的PHP代码:
<?php
echo 'MESSAGE:';
print_r($_POST); ?>
And here is my Java code on the client: 这是客户端上的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();
}
No, $_POST array is being populated only with multipart/form-data
enctype. 不,仅使用multipart/form-data
编码类型填充$ _POST数组。
you can use either fopen() with php://input wrapper or $HTTP_RAW_POST_DATA 您可以将fopen()与php:// input包装器一起使用或$ HTTP_RAW_POST_DATA
http://www.php.net/manual/en/wrappers.php.php http://www.php.net/manual/en/wrappers.php.php
Try viewing the page's source in your browser after you post the xml, the xml isn't going to show up if you are viewing the page normally unless you pass it to htmlspecialchars(). 发布xml后,尝试在浏览器中查看页面的源代码,如果您正常查看页面,则除非将其传递给htmlspecialchars(),否则xml不会显示。 print_r($_POST);
is xss so be careful. 是xss,所以要小心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.