简体   繁体   English

如何使用HTTP POST从Java应用程序发送XML并在服务器上使用PHP打印它?

[英]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.

相关问题 如何在java中的http post中发送json对象 - How can I send json object in http post in java 如何使用Java应用程序将本地.png发送到服务器上的.php文件? - How to send local .png to .php file on server using java application? 无法在 Android Studio 中使用 HTTP POST 将 JSON 发送到我的服务器 - Can't send JSON to my server using HTTP POST in Android Studio with java 如何将图像从Java应用程序发送到Android应用程序? - How can I send an Image from a Java application to an Android application? 如何在android上使用java发出http post请求 - How can I make an http post request using java on android 如何使用Java将数据发布到HTTP服务器 - How to post data to HTTP server using Java 如何使用http POST JAX-RS将对象从AngularJS服务传递给Java - How can I pass an Object from an AngularJS Service to Java using http POST JAX-RS 如何从Java的后台服务发送POST请求? - How can I send POST requests from a background service in Java? 当我向服务器发送http post请求时,如何像Java代码一样实现linux c中的目标? - How to achieve the target in linux c as the java code do when I send a http post request to a server? 如何将多个信息从客户端应用程序发送到服务器? - How can i send multiple information from a client application to a server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM