本文介绍了如何从XML http请求中读取XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在网络应用上执行某项操作时,它会执行ajax调用或沿该行返回XML格式的某些数据。当我单击浏览器中的On Inspect元素并单击网络选项卡时,我可以看到所请求的XML响应数据。请参阅:

When I perform a certain action on a web app, it performs a ajax call or something along that line that returns some data in XML format. When I click On Inspect element in the browser and click on the networks tab, I can see the XML response data that was requested. see:

我尝试在java中执行http请求以检索此XML数据。这是java代码:

I tried to perform a http request in java to retrieve this XML data. Here is the java code:

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。请参阅:

For the URL I am creating a http request, I am using the exact same request URL in the ajax calls header. see:

当我执行GET请求时,我收到的响应代码为200,我认为请求成功,但在我的日志中它显示没有XML,即使我尝试打印出来。日志显示如下:

When I perform the GET request, I received a response code of 200 which I assume the request was successful, but on my log it displays no XML even though I try to print it out. The log displayed this:

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

我还应该注意,当我尝试转到请求时直接在浏览器上的URL它只是一个空白的白页。

I should also note that when I try to go to the request URL directly on the browser it's just a blank white page.

当我使用包含网页的请求URL时,http请求返回DOM中的所有html和js 。但是我试图使用的请求URL只是一个空白页面,即使有一些xml数据,也会向我返回一个空白响应。我究竟做错了什么?为什么我无法检索和显示XML?在显示XML之前是否需要解析XML?

When I use a request URL that contains a webpage, the http request returns all the html and js in the DOM. But the request URL I was trying to use which is just a blank page returns a blank response to me even though there are suspose to be some xml data. What am I doing wrong? Why is it that I cannot retrieve and display the XML? Do I need to parse the XML before it can be displayed?

推荐答案

我通过将其更改为POST请求解决了问题(和浏览器一样)。我还包括与该请求相关联的表单数据,如图所示。

I solved the issue by changing it to a POST request (same as what the browser did). I also included the form data associated with that request as shown in the pictures up there.

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;
}

这篇关于如何从XML http请求中读取XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:38