Parsing JSP page content using Pattern and Matcher
<p>I am parsing a JSP page to get the values of some <code>HTML</code> <code>inputText</code> and <code>id</code> which I have to send further as a <code>POST</code> request parameters. The problem is that out of 5 times only 1 time I am getting right parsed values. Below is my <code>Java</code> code, which is used to get the whole page content</p>

<pre><code>private StringBuilder getPageContent(String id, String str, int len) {
    StringBuilder postParams = null;

    try {
        URL urlGet = new URL(homeUrl + "home.jsp?id=" + token);

        HttpURLConnection getSendCon = (HttpURLConnection) urlGet
                .openConnection();

        getSendCon.setDoOutput(true);
        getSendCon
                .setRequestProperty("User-Agent",
                        "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0");
        getSendCon.setRequestProperty("Content-Type", " text/html");
        getSendCon
                .setRequestProperty("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        getSendCon.setRequestProperty("Cookie", cookie);
        getSendCon.setRequestProperty("Accept-Encoding", "gzip,deflate");
        getSendCon.setRequestProperty("Referer", homeUrl
                + "Main.action?id=" + token);
        getSendCon.setRequestProperty("Connection", " Keep-Alive");
        getSendCon.setRequestProperty("Accept-Encoding", " gzip,deflate");
        getSendCon.setRequestProperty("Keep-Alive", " 300");
        getSendCon.setRequestMethod("GET");
        getSendCon.setInstanceFollowRedirects(false);

        InputStreamReader it = new InputStreamReader(
                getSendCon.getInputStream(), "UTF-8");

        postParams = new StringBuilder();

        int t;
        while ((t = it.read()) != -1)
            postParams.append((char) t);

        it.close();

    } catch (MalformedURLException e) {
        System.err.println("Error : " + e.getMessage());
    } catch (IOException e) {
        System.err.println("Error : " + e.getMessage());
    }

    return postParams;
}
</code></pre>

<p>Below is the <code>Pattern</code> and <code>Matcher</code> to parse the content, which is returned by above code-</p>

<pre><code>StringBuilder allParams = getPageContent(token, num, msg.length());

if (allParams == null) {
    System.err.println("Error while getting page content");
    return false;
}
else {

        final String regex = "&lt;input.*inputName=['\"](\\w+)['\"].*inputValue=['\"]([\\w:.\\s]+)['\"].*";
        final Pattern pattern = Pattern.compile(regex);

        Matcher matcher = pattern.matcher(allParams);

        String params = "";

        while (matcher.find()) {

            String inputName = matcher.group(1);
            String inputValue = matcher.group(2);

            if (!inputName.trim().isEmpty())
                params += inputName + '=' + inputValue + '&amp;';
        }
        System.out.println("parsed inputValues=[" + params + "]");
}
</code></pre>

<p>Every time i am not getting right <code>params</code>. Where is the problem? Is there any better way to read the values from a webpage?</p>

<p>Also <code>StringBuilder</code> is returning garbage characters which are not readable even <code>UTF8</code> is being used. Why?</p>
