从Java中的HTML http响应中读取参数值

时间:2018-07-26 12:07:00

标签: java html http httpresponse

我的http回复

HttpResponse response = httpClient.execute(request);
                // Read response using StringBuilder
                StringBuilder sb = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                  response.getEntity().getContent()), 65728);
                String line = null;
                while ((line = reader.readLine()) != null) {
                sb.append(line);
                }
                // Print response
                System.out.println(sb.toString());

服务器的响应如下所示

<!DOCTYPE html><html>    <head>        <title>Redirecting</title>    </head>    <body>                                                <script>                    function populateAndSubmitForm()                    {                        if(!document.forms[0].PaRes.value) {                            document.forms[0].PaRes.value = window.parent.timeoutRedirectInformation.TimeoutPaRes;                        }                        document.forms[0].MD.value = window.parent.timeoutRedirectInformation.MD;                        document.forms[0].action = window.parent.timeoutRedirectInformation.Term;                        document.forms[0].submit();                    }                </script>                <form name="RedirectToTermUrlForm" id="RedirectToTermUrlForm" method="post" target="_self">                    <input type="hidden" name="MD">                    <input type="hidden" name="PaRes" value="xyz">                </form>                <script>                    populateAndSubmitForm();                </script>                 <noscript>                    <div style="font-align:center;">                        <h2 style="color:red;">JavaScript is currently disabled or is not supported by your browser.</h2>                        <h3 style="color:red;">Please enable JavaScript to continue.</h3>                    </div>                </noscript>                        </body></html>

我需要读取PaRes的值,所以我需要xyz值。我尝试使用Enumeration和getParam,但是我总是以null结尾。请告知出路。谢谢。

1 个答案:

答案 0 :(得分:1)

Document doc = Jsoup.parse(sb.toString());
                Element element = doc.select("input[name=PaRes]").first();
                String inputValue = element.attr("value");
                System.out.println("PaRes    "+inputValue);