簡體   English   中英

如何從XML http請求中讀取XML?

[英]How do I read in the XML for from java http request?

當我在Web應用程序上執行某個操作時,它會執行ajax調用或沿該行返回XML格式的某些數據。 當我單擊瀏覽器中的On Inspect元素並單擊網絡選項卡時,我可以看到所請求的XML響應數據。 看到: 在此輸入圖像描述

我試圖在java中執行http請求以檢索此XML數據。 這是java代碼:

private StringBuffer sendGet() {

    final String USER_AGENT = "Mozilla/5.0";
    final String CONTENT_LENGTH = "131";
    StringBuffer response = new StringBuffer();
    String url = "https://same as the request header";

    try {
        //create the http connection
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent",USER_AGENT);
        con.setRequestProperty("Content-Length",CONTENT_LENGTH);

        int responseCode = con.getResponseCode();
        System.out.println("Sending 'GET' request to URL " + url);
        System.out.println("Response Code:" + responseCode);

        //read in the get reponse
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;

        while((inputLine = in.readLine()) != null) {
            response.append(inputLine.toString());
        }

        //close input stream
        in.close();

        System.out.println("response is: " + response);

    } catch (MalformedURLException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;
}

對於我正在創建http請求的URL,我在ajax調用頭中使用完全相同的請求URL。 看到: 在此輸入圖像描述

當我執行GET請求時,我收到的響應代碼為200,我認為請求成功,但在我的日志中,即使我嘗試將其打印出來,它也不會顯示XML。 日志顯示如下:

Sending 'GET' request to URL https:"blah blah blah"
Response Code:200
response is: 

我還應該注意,當我嘗試直接在瀏覽器上訪問請求URL時,它只是一個空白的白頁。

當我使用包含網頁的請求URL時,http請求返回DOM中的所有html和js。 但是我試圖使用的請求URL只是一個空白頁面,即使有一些xml數據,也會給我一個空白的回復。 我究竟做錯了什么? 為什么我無法檢索和顯示XML? 在顯示XML之前是否需要解析XML?

我通過將其更改為POST請求(與瀏覽器的操作相同)解決了該問題。 我還包括與該請求相關聯的表單數據,如圖中所示。

private StringBuffer sendPost() {

    StringBuffer response = new StringBuffer();
    String url = "https://example.com/snowbound/AjaxServlet";
    final String CONTENT_LENGTH = "131";
    final String CONTENT_TYPE = "application/x-www-form-urlencoded";
    final String ACCEPT_LANGUAGE = "en-US,en;q=0.8";

    try {
        //create http connection
        URL obj = new URL(url);
        HttpsURLConnection connection = (HttpsURLConnection) obj.openConnection();

        //add request header
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty("Accept-Language", ACCEPT_LANGUAGE);
        connection.setRequestProperty("Content-Type", CONTENT_TYPE);
        connection.setRequestProperty("Content-Length", CONTENT_LENGTH);

        DataOutputStream output = new DataOutputStream(connection.getOutputStream());

        //form data
        String content = "documentId=3896&action=getAnnotationModel&annotationLayer=1&pageCount=1&pageIndex=0";

        //write output stream and close output stream
        output.writeBytes(content);
        output.flush();
        output.close();

        //read in the response data
        BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        while((inputLine = input.readLine()) != null) {
            response.append(inputLine.toString());
        }

        //close input stream
        input.close();

        //print out content
        int responseCode = connection.getResponseCode();
        System.out.println("response code: " + responseCode);
        System.out.println("respone is: " + response);

    } catch (MalformedURLException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;
}

使用DocumentBuilder : -

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(url).openStream());

用於打印XML : -

DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //pretty printing
transformer.transform(domSource, result);
System.out.println("XML IN String format is: \n" + writer.toString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM