简体   繁体   中英

Get POST Content from HttpServletRequest

I want to read out the data from a POST request in a Java Servlet . This servlet runs on the Geoserver as service and called with the URL.


POST Request (create with python script):

POST http://localhost:8080/geoserver/ows?request=upload&service=GeoTransfer&version=1.0.0 HTTP/1.1
Host: localhost:8080
User-Agent: python-requests/2.20.0
Accept-Encoding: gzip, deflate
Accept: application/xml
Connection: keep-alive
Authorization: admin:admin
fileName: transfer.xml
Content-Type: application/xml
Content-Length: 489

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<transfer>
  <enviroment>Test</enviroment>
  <product>Testproduct</product>
  <user>admin</user>
</transfer>

Java servlet:

public void upload(HttpServletRequest request, httpServletResponse response)
            throws ServletException, IOException {

        System.out.println("===== Begin headers =====");
        Enumeration<String> names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String headerName = names.nextElement();
            System.out.println(headerName + " = " + request.getHeader(headerName));
        }
        System.out.println("===== End headers =====\n");


        System.out.println("===== Begin Body =====");
        if ("POST".equalsIgnoreCase(request.getMethod())) {

            try {
                Scanner s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\\A");
                System.out.println(s.hasNext() ? s.next() : "");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("===== End Body =====\n");

 }

Output:

===== Begin headers =====
Authorization = admin:admin
fileName = transfer.xml
Accept = application/xml
User-Agent = python-requests/2.20.0
Connection = keep-alive
Host = localhost:8080
Accept-Encoding = gzip, deflate
Content-Length = 489
Content-Type = application/xml
===== End headers =====

===== Begin Body =====

===== End Body =====

Problem:

Why i cant read out the xml data body? Is something wrong with the request and the data dont pass to Java?

EDIT1:

Try other Methods with Inputstream, but the body is always a empty line. Could be the Problem that the Server ignore the Body and use only the header if he create the request object ?

Edit2:

Try Majid Khakwani answer and get empty string. I print out request.getContentLength() and it is 489 . Did the method get this number out of the header or the real length of the content from object request?

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                String xml = null;
                try {
                        byte[] xmlData = new byte[request.getContentLength()];

                        //Start reading XML Request as a Stream of Bytes
                        InputStream sis = request.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(sis);

                        bis.read(xmlData, 0, xmlData.length);

                        if (request.getCharacterEncoding() != null) {
                                xml = new String(xmlData, request.getCharacterEncoding());
                        } else {
                                xml = new String(xmlData);
                        }
                        //xml and xmlData contains incomplete data
                } catch (IOException ioe) {
                   ...
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM