2012-03-07 1 views
2

나는 java htmlunit의 초보자이므로 어떤 도움을 주시면 감사하겠습니다. 미리 감사드립니다.HTML 단위 - 양식을 사용하여 웹 사이트를 보호하기위한 로그인 - 양식을 작성한 후 페이지에 연결할 수 없습니다.

htmlunit을 사용하여 웹 페이지의 양식에 사용자 이름과 암호를 제출하여 웹 브라우저의 동작을 미러링하여 사용자 이름 및 비밀번호 인증으로 보호되는 웹 페이지에 로그인하려고합니다. 웹 사이트 자체에는 양식 기반 승인이 있습니다. 나는 코드의 쿠키 부분없이 코드를 실행하면

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.util.Iterator; 
import java.util.Set; 

//Import htmlunit classes  

import com.gargoylesoftware.htmlunit.WebClient; 
import com.gargoylesoftware.htmlunit.html.HtmlForm; 
import com.gargoylesoftware.htmlunit.html.HtmlPage; 
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; 
import com.gargoylesoftware.htmlunit.html.HtmlTextInput; 
import com.gargoylesoftware.htmlunit.util.Cookie; 


//This Class attempts to submit user and password credentials 
//and mirrors how a login button would be clicked on a webpage: 

public class submitForm { 

    public static void main(String[] args) throws Exception { 

     WebClient webClient = new WebClient(); 

     // Get the first page 
     HtmlPage page1 = (HtmlPage) webClient.getPage("http://cmdbjr/frameset.php?ci_name=&ci_id=&ci_type="); 

     // Get the form that we are dealing with and within that form, 
     // find the submit button and the field that we want to change. 
     HtmlForm form = page1.getFormByName("loginform"); 

     // Enter login and passwd 
     form.getInputByName("user_id").setValueAttribute("#####"); 
     form.getInputByName("password").setValueAttribute("#####"); 

     // Click "Sign In" button/link 
     page1 = (HtmlPage) form.getInputByValue("Log In").click(); 

     // I added the cookie section but this returns a null pointer exception  
     Set<Cookie> cookie = webClient.getCookieManager().getCookies(); 

     if(cookie != null){ 

      Iterator<Cookie> i = cookie.iterator(); 

      while (i.hasNext()) { 

       webClient.getCookieManager().addCookie(i.next()); 

      } 

     } 


     // Get page as Html 
     String htmlBody = page1.getWebResponse().getContentAsString(); 
     // Save the response in a file 
     String filePath = "c:/temp/test_out.html"; 

     BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filePath))); 
     bw.write(htmlBody); 
     bw.close(); 

     // Change the value of the text field 
     // userField.setValueAttribute("alwalsh"); 
     // passwordField.setValueAttribute("1REland6"); 

     // Now submit the form by clicking the button and get back the second page. 
     // final HtmlPage page2 = button.click(); 


     webClient.closeAllWindows(); 
    } 
} 

내가 오류 페이지가 표시되지 않습니다 로그인 페이지 이후 인 도달하려고하고있는 페이지는 인터넷에 연결되지 말에 나타납니다.

코드는 쿠키 섹션 오류 실행하는 경우 :

Exception in thread "main" >java.lang.NullPointerException at contentWeb.main(contentWeb.java:26)

가 반환됩니다.

나는 모든 도움이 크게 감사하겠습니다. 자바 htmlunit을 처음 사용합니다. 미리 감사드립니다.

답변

1

필자의 yahoo 메일 로그인 자격 증명을 사용하여 예제를 복제했으며 효과가있었습니다. 그러나 나는 스크립트 오류에 대한 예외를 무시하기 위해 webClient.setThrowExceptionOnScriptError(false);을 추가했습니다.

관련 문제