简体   繁体   中英

Execute second POST method JSoup

I want to extract the wireless signals on the radar of my modem. For this, I'm trying to connect vía web to my modem using JSoup. To do this, I follow the next steps:

  1. Login with the credentials.
  2. Visit the inner link Wifi Survey.
  3. Press the scan button.

After the scan button, I have to wait at least 5 seconds to get the list of wireless near. This is what I have so far:

Extraccion.java

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;


import javax.net.ssl.*;
import java.awt.*;
import java.io.IOException;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Map;


public class Extraccion {

    private void disableSSLCertCheck() throws NoSuchAlgorithmException, KeyManagementException {
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }
        };

        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    }

    public static void main(String[] args) throws IOException, KeyManagementException, NoSuchAlgorithmException, AWTException {
        Robot r = new Robot();
        Extraccion e = new Extraccion();
        e.disableSSLCertCheck();

        // Here I get the form content '/goform/login'
        Connection.Response loginForm = Jsoup.connect("https://ip_address/goform/login")
                .method(Connection.Method.GET)
                .data("cookieexists", "false")
                .data("user", "admin")
                .data("pws", "admin")
                .data("Login", "Login")
                //.cookies(loginCookies)
                .method(Connection.Method.POST)
                .execute();

        Map<String, String> loginCookies = loginForm.cookies();
        //r.delay(5000);
        Document document2 = Jsoup.connect("https://ip_address/admin/feat-lan-ip.asp")
                .cookies(loginCookies)
                .get();
        Element tableDHCP = document2.getElementById("connected_computers");
        System.out.println("Printing connected devices");
        System.out.println(tableDHCP);

        Connection.Response submitButton = Jsoup.connect("https://ip_address/goform/WlsRadar")
                .data("setRadar", "scan")
                .data("dir", "admin/")
                .data("file", "wireless_radar")
                .cookies(loginCookies)
                .method(Connection.Method.POST)
                .execute();
        r.delay(20000);
        loginCookies = submitButton.cookies();
        document2 = Jsoup.connect("https://ip_address/admin/wireless_radar.asp")
                .cookies(loginCookies)
                .get();
        Element tableWireless_radar = document2.getElementById("maincontent");
        System.out.println("Printing radar wireless");
        System.out.println(tableWireless_radar);
    }
}

I can login into the page, but I'm not able to press the scan button. This is how the section page of the button looks like:

wireless_radar.asp

<form name="RadarForm" action="/goform/WlsRadar" method="post">
<input type="hidden" value="admin/" name="dir">
<input type="hidden" value="wireless_radar" name="file">
<input type="hidden" value="" name="setRadar">
<p id="introduction"></p>

<p id="title1"><span class="LANGS" id="Wireless_WifiSiteSurvey_Title">Survey Result</span></p>
<div id="content1">
<table class="checklist1" id="wlsRadar_table">
<tbody><tr class="item">
<td class="wireless_radar2"><span class="LANGS" id="Wireless_WifiSiteSurvey_Channel">ch</span></td>
<td class="wireless_radar6"><span class="LANGS" id="Wireless_WifiSiteSurvey_Ssid">SSID</span></td>
<td class="wireless_radar7"><span class="LANGS" id="Wireless_WifiSiteSurvey_Bssid">BSSID</span></td>
<td class="wireless_radar8"><span class="LANGS" id="Wireless_WifiSiteSurvey_Security">Security</span></td>
<td class="wireless_radar3"><span class="LANGS" id="Wireless_WifiSiteSurvey_siganl">signal(%)</span></td>
<td class="wireless_radar4"><span class="LANGS" id="Wireless_WifiSiteSurvey_Wmode">W-mode</span></td>
<td class="wireless_radar4"><span class="LANGS" id="Wireless_WifiSiteSurvey_ExtenelChannel">ExtCH</span></td>
<td class="wireless_radar2"><span class="LANGS" id="Wireless_WifiSiteSurvey_Inservice">NT</span></td>
<td class="wireless_radar5"><span class="LANGS" id="Wireless_WifiSiteSurvey_WPS">WPS DPID</span></td>
</tr>
</tbody></table>

</div>  <!--end of content1-->
<div id="content1">
<center>
<span class="LANGS" id="Wireless_WifiSiteSurvey_ScanButton"><input type="submit" class="button" value="Scan" onclick="ScanRadar()"></span>
<span class="LANGS" id="Wireless_WifiSiteSurvey_ClearButton"><input type="submit" class="button" value="Clear" onclick="clearResult()"></span>
<center>
</center></center></div>
</form>
</div>

The button I'm trying to press is Scan, but it seems that doesn't have an ID. Any idea how It could work?

PS: Everytime I click on Scan button, this code:

<input type="hidden" value="" name="setRadar">

changes into:

<input type="hidden" value="scan" name="setRadar">

I do not know your router, but it seems that the wifi survey website performs some JavaScript. JSoup does not run JavaScript. It is not a browser. It simply parses HTML and creates a DOM representation of that HTML in memory.

You either want to use a real browser and control that via Java (see at selenium webdriver for example) or you can analyze what the JavaScript actually does and it it triggers an AJAX call or something. JSoup can be used to mimic such AJAX calls, if you can reproduce all parameters correctly. Use the browser developer's tools to find out what actually goes on on the routers website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM