从我的响应数据中获取特定的字符串值

时间:2015-11-05 09:11:07

标签: java

我从我的java代码获取响应数据,我点击URL如下,

    String url = "http://www.google.com/search?q=mkyong";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("User-Agent", USER_AGENT);

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

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

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

    //print result
    System.out.println(response.toString());

我将html数据作为字符串值...

我想从此响应数据中获取隐藏值。

    <input type="hidden" name="HREF.DUMMY.MENSYS.1"
        value="-X-tB--3TF8LlA02j-LKYRAT75rwYwwchuvSyZ9vWVwQ0"
        id="url" />

如何获得此值?

1 个答案:

答案 0 :(得分:0)

你可以,

只需在响应中使用以下代码,

    Document doc = Jsoup.connect(response.toString());
    Element el= doc.select("type[hidden]");
    Streing val = el.getAttr("value");

然后你可以从doc对象中找到你的元素。