2012-10-14 3 views
2

사용자가 필요하고 통과해야하는 웹 페이지에서 파일을 다운로드하고 싶지만 먼저 쿠키를 얻어야합니다. 내가하고 싶은 것은이 Python 스크립트에 정확하게 설명되어 있지만 Java를 사용하여 수행하려고합니다.자바로 쿠키가 포함 된 웹 페이지에 로그인하십시오.

나는 그것에 대해 읽었으며, 나는 httpclient 라이브러리를 발견했다. 내가 필요로하는 것은 모두 httpclient인가? 기계어 및 urllib2 자바에 대한 모든 해당 라이브러리?

미리 감사드립니다.

#!/usr/bin/python 

import mechanize, urllib2 
from urllib import urlopen, urlencode 

user = 'username' 
password = 'password' 
output_file = 'name.pdf' 

web = "https://..." 
bills_page = "https://.../bills" 
login_web = "https://.../login/" 
file = "https://.../file_I_want" 

br = mechanize.Browser() 
br.open(web) 

data = { 
    'user_username': user, 
    'user_password': password, 
    'idClientehidden': '', 
    'answer': '' 
} 

response1 = urllib2.Request(login_web, urlencode(data)) 

br.open(response1) 
br.open(bills_page) 
html_bills = br.response().read() 

br.open(file) 
pdf_bill = open(output_file, 'w') 
pdf_bill.write(br.response().read()) 
pdf_bill.close() 
+0

쿠키 읽기에 관한 모든 것이 궁금하십니까? 또는 외부 웹에 로그인하여 파일을 다운로드 할 방법을 찾고 있습니까? –

+0

외부 웹에 로그인하고 파일을 다운로드하는 방법. 제 질문은 내가 httpclient를 사용해야하거나 내 목표를 위해 뭔가를 더 잘 안다면입니다. –

답변

2

HttpClient 쿠키 취급 및 인증 URL을 액세스하기위한 좋은 프레임 워크입니다. 또한

당신은 아래 Authenticator, URLBufferedReader으로 핵심 자바 구성 요소를 사용할 수 있습니다

public class HTTPAuthenticator extends Authenticator { 

    protected PasswordAuthentication getPasswordAuthentication() { 
    String username = "user"; //<--read from cookie 
    String password = "password"; //<--read from cookie 
    return new PasswordAuthentication(username, password.toCharArray()); 
    } 
} 
  • 사용자 지정 설정 쿠키에서 userId/Password을 읽을 것이다

    1. 사용자 정의 인증 프로그램을 만들기를 인증 자 HTTPAuthenticator을 기본 인증 자로 사용하십시오. 일단 완료

      Authenticator.setDefault(new HTTPAuthenticator()); 
      
    2. 는 파일을 읽고 다음과 같이 로컬 드라이브에 쓰기 :이 도움이

      URL url = new URL("http://secureweb/secure.html"); 
      BufferedReader br= new BufferedReader(new InputStreamReader(url.openStream())); 
      File file = new File("myLocalFile"); 
      BufferedWriter bw = new BufferedWriter (file); 
      String lineStr; 
      while ((str = br.readLine()) != null) { 
          bw.write(); 
      } 
      bw.close(); 
      br.close(); 
      

    희망을.

  • 관련 문제