2012-10-30 48 views
0

我有一个bash脚本,当我登录网页,然后解析HTML。我使用的命令是wget:发送邮件与Java

wget --save-cookies=cookies.txt --post-data "uid=USER&pass=PWD" http://www.spanishtracker.com/login.php 

wget --load-cookies=cookies.txt "http://www.spanishtracker.com/torrents.php" -O OUTPUT 

现在,我试图用Java来创建这些命令。首先,我试图发布请求,但是当我执行输出时,并没有给我记录。这些是Java的代码:

try { 
     data = URLEncoder.encode("uid", "UTF-8") + "=" + URLEncoder.encode("USER", "UTF-8"); 
     data += "&" + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode("PASS", "UTF-8"); 

       // Send the request 
       URL url = new URL("http://www.spanishtracker.com/index.php"); 
       URLConnection conn = url.openConnection(); 


       conn.setDoOutput(true); 

       OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); 

       //write parameters 
       writer.write(data); 
       writer.flush(); 

       // Get the response 
       StringBuffer answer = new StringBuffer(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        answer.append(line); 
       } 
       writer.close(); 
       reader.close(); 
       // temporary to build request cookie header 
       StringBuilder sb = new StringBuilder(); 

       // find the cookies in the response header from the first request 
       List<String> cookies = conn.getHeaderFields().get("Set-Cookie"); 
       if (cookies != null) { 
        System.out.println("Hay cookies para guardar"); 
        for (String cookie : cookies) { 
         if (sb.length() > 0) { 
          sb.append("; "); 
         } 

         // only want the first part of the cookie header that has the value 
         String value = cookie.split(";")[0]; 
         sb.append(value); 
        } 
       } 

请问你能否帮助我。 非常感谢和抱歉我的英语!

回答