Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java代码在用作库时无法按预期工作_Java_Htmlunit - Fatal编程技术网

Java代码在用作库时无法按预期工作

Java代码在用作库时无法按预期工作,java,htmlunit,Java,Htmlunit,我制作了一个简单的程序来登录instagram网站 我的原始代码在java主类中工作,但现在我遇到了一个奇怪的问题,当我把代码放在另一个类中时,我在另一个类中使用它,因此我可以从不同的包中加载并在其他类中使用它,不再访问该站点 我只是对代码做了一些微不足道的更改,比如接收一些参数,比如用户名和密码 主类中的原始代码: WebClient webClient = new WebClient(BrowserVersion.CHROME); //configuracion de op

我制作了一个简单的程序来登录instagram网站

我的原始代码在java主类中工作,但现在我遇到了一个奇怪的问题,当我把代码放在另一个类中时,我在另一个类中使用它,因此我可以从不同的包中加载并在其他类中使用它,不再访问该站点

我只是对代码做了一些微不足道的更改,比如接收一些参数,比如用户名和密码

主类中的原始代码:

WebClient webClient = new WebClient(BrowserVersion.CHROME);

        //configuracion de opciones de webbrowser
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getCookieManager().setCookiesEnabled(true);

        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);

        HtmlPage page = (HtmlPage) webClient.getPage(authorizationUrl);
        WebResponse response = page.getWebResponse();

        response.cleanUp(); 



       final HtmlForm loginForm = page.getForms().get(0);

     // revisar si la sesion ha expirado
       if(loginForm!=null){ //la session ha expirado relogear

              //se pone el usuario en el input html por nombre
              final HtmlTextInput txtUser = loginForm.getInputByName(usuarioInputName);
              txtUser.setValueAttribute("oestedev");

              //se pone el password en el input html por nombre
              final HtmlPasswordInput txtpass = loginForm.getInputByName(passwordInputName);
              txtpass.setValueAttribute("oeste18");

              //boton submit por valor
             final HtmlSubmitInput submitLogin = loginForm.getInputByValue(submitLoginButtonValue);

             final HtmlPage returnPage = submitLogin.click(); 

             System.out.println( returnPage.getUrl());


             String urlAcceso = returnPage.getUrl().toString();
             String[] parteUrl = urlAcceso.split("=");
            // String urlAuth = parteUrl[0]; // url de autorizacion
             codigo = parteUrl[1]; // codigo para obtener el access token

             System.out.println(codigo);



        }


        webClient.close();
从我用作库的类:

public void realizarConexion(String usuario,String password) throws IOException{

        String authorizationUrl = service.getAuthorizationUrl(TOKEN_ACCESO);
        String codigo = null;
        WebClient webClient = new WebClient(BrowserVersion.CHROME);

        //configuracion de opciones de webbrowser
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setUseInsecureSSL(true);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getCookieManager().setCookiesEnabled(true);

            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.getOptions().setRedirectEnabled(true);

            //acceso a la url desde webclient


                  HtmlPage page = (HtmlPage) webClient.getPage(authorizationUrl);
                  WebResponse response = page.getWebResponse();

                    response.cleanUp(); 

                   final HtmlForm loginForm = page.getForms().get(0);

                   // try to login to the site
                   if(loginForm!=null){ //la session ha expirado relogear

                          //se pone el usuario en el input html por nombre
                            HtmlTextInput txtUser = loginForm.getInputByName(usuarioInputName);
                          txtUser.setValueAttribute(usuario);

                          //se pone el password en el input html por nombre
                            HtmlPasswordInput txtpass = loginForm.getInputByName(passwordInputName);
                          txtpass.setValueAttribute(password);

                          //boton submit por valor
                           HtmlSubmitInput submitLogin = loginForm.getInputByValue(submitLoginButtonValue);

                         //accede al sitio
                          HtmlPage returnPage = submitLogin.click();      

                         String urlAcceso = returnPage.getUrl().toString();
                         System.out.println(urlAcceso);// to check if i got to the correct url
                         String[] parteUrl = urlAcceso.split("=");

                         codigo = parteUrl[1]; // codigo para obtener el access token
                   }

                Verifier verifier = new Verifier(codigo);
                Token accessToken = service.getAccessToken(TOKEN_ACCESO, verifier);

                instagram = new Instagram(accessToken);

                webClient.close();

    }

不访问该站点意味着什么?错误日志?例外?你确定你的iFloginfo=空{}块是否为真?意思是submitLogin.click;发生了什么?@Vovka异常,但因为授权码不正确one@TheNeo Noir Developer是的,如果我打印表单的内容,很明显我是从网页获取表单的。您可能有冲突的依赖项,从HtmlUnit需要的jar开始,然后逐渐添加其他jar。