繁体   English   中英

从HttpServletRequest获取POST内容

[英]Get POST Content from HttpServletRequest

我想从Java Servlet中POST请求中读取数据。 该servlet作为服务运行在Geoserver上,并使用URL进行调用。


POST请求(使用python脚本创建):

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");

 }

输出:

===== 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 =====

问题:

为什么我无法读出xml数据正文? 请求有问题,数据没有传递给Java吗?

EDIT1:

使用Inputstream尝试其他方法,但主体始终为空行。 如果服务器创建请求对象,服务器会忽略主体而仅使用标头,可能是这样的问题吗?

EDIT2:

尝试Majid Khakwani答案并获取空字符串。 我打印出request.getContentLength() ,它是489 该方法是否从对象请求的标题或内容的实际长度中获取了此数字?

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) {
                   ...
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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