执行第二个POST方法JSoup

时间:2018-02-13 20:50:31

标签: java jsoup

我想在调制解调器的雷达上提取无线信号。为此,我正在尝试使用JSoup将víaweb连接到我的调制解调器。为此,我按照以下步骤操作:

  1. 使用凭据登录。
  2. 访问内部链接Wifi调查。
  3. 按扫描按钮。
  4. 扫描按钮后,我必须等待至少5秒才能获得无线附近的列表。这就是我到目前为止所做的:

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

    我可以登录页面,但我无法按下扫描按钮。这就是按钮的截面页面的样子:

    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>
    

    我试图按下的按钮是扫描,但似乎没有ID。知道它是如何工作的吗?

    PS:每次我点击扫描按钮,这段代码:

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

    变为:

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

1 个答案:

答案 0 :(得分:0)

我不知道你的路由器,但似乎wifi调查网站执行了一些JavaScript。 JSoup不运行JavaScript。它不是浏览器。它只是解析HTML并在内存中创建该HTML的DOM表示。

您要么使用真正的浏览器并通过Java控制(例如,请参阅selenium webdriver),或者您可以分析JavaScript实际执行的操作,并触发AJAX调用等。如果可以正确地重现所有参数,JSoup可用于模仿此类AJAX调用。使用浏览器开发人员的工具找出路由器网站上实际发生的情况。