2011-07-27 5 views
1

나는이 때 website에 사용자 이름과 암호를 삽입하고 로그인 할 때 계정에서 데이터를 가져 오기 위해 java를 사용하는 프로그램을 만들려고 노력하고 있습니다. Google을 통해 많은 검색을 시도했지만 일부는 시도했지만 했지만 이것을 달성하기 위해서는 많은 행운이 필요합니다. 어떤 제안?자바를 통해 웹 사이트에 로그인 하시겠습니까?

+0

어떤 오류 메시지가 나타납니다? – Jonas

+0

나는 어떤 오류도 내지 않고 그냥이 메시지를 받았다. HTPP POST를 성공적으로 만들었고 응답받은 것은 다음과 같다. 페이지의 내용이 뒤 따른다. –

+0

지금 사용하고있는 코드를 보여줘. – Marcelo

답변

2

사용중인 코드는 오래된 기술입니다. Commons HttpClient은 훨씬 간단하고 강력한 옵션이며 많은 예제와 온라인 설명서가 있습니다. 루비를 싫어하지 않는다면 나는 Watir을 던집니다. 또는 실제로 코드가 필요 없으며 단순한 레코드 재생 자동화를 원하면 Selenium이 좋은 옵션입니다.

0

언어 이해가 부족하여 구체적인 조언을 드릴 수 없습니다.

그러나가는 길은 웹 페이지에서 양식을 확인하는 것입니다. 그런 다음 여기에 정의 된 사이트에 게시물 요청을 보낼 수 있습니다.

게시물 본문으로 URL 매개 변수로 표시되는 것처럼 양식의 매개 변수를 보냅니다.

<!-- language: lang-js --> 
    String payload = "password=test&user=test" // password and user must be replaced with the names of the form fields and test with password and username respectively 
    HttpURLConnection configConnection = new URL(formTarget).openConnection(); 
    System.out.println("+------------------------------------------------------"); 
    System.out.println("| Request "); 
    System.out.println("+------------------------------------------------------"); 
    System.out.println("| URL: " + configConnection.getURL()); 
    System.out.println("+------------------------------------------------------"); 
    System.out.println("| Payload "); 
    System.out.println("+------------------------------------------------------"); 
    System.out.println(payload); 
    configConnection.setDoOutput(true); 
    configConnection.setRequestMethod("POST"); 

    configConnection.getOutputStream().write(payload.getBytes()); 
    InputStream xmlInputStream = configConnection.getInputStream(); 

    int _byte = -1; 
    ByteArrayOutputStream bios = new ByteArrayOutputStream(); 
    while ((_byte = xmlInputStream.read()) > -1) { 
     bios.write(_byte); 
    } 
    final byte[] responseBytes = bios.toByteArray(); 
    String responseString = new String(responseBytes); 
    System.out.println("+------------------------------------------------------"); 
    System.out.println("| Response "); 
    System.out.println("+------------------------------------------------------"); 
    System.out.println("| Code: " + configConnection.getResponseCode()); 
    System.out.println("+------------------------------------------------------"); 
    System.out.println(responseString); 
    System.out.println("+------------------------------------------------------"); 

더 나은 당신이 적절한 XHTML로 변환하는 tagsoup 라이브러리를 사용할 수있는 출력을 구문 분석합니다.

+0

그런 메소드가 없습니다 : –

+0

configConnection.getOutputStream(). write (payload.getBytes()); // 내 예제에서 이것을 바꾸는 것을 잊었다 – DoubleMalt

+0

'isCompressed()'는 어떨까요? –

관련 문제