簡體   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