2015-06-14 151 views
0

我想编写一个Java应用程序,该应用程序登录到一个网站,然后通过cookie身份验证导航到不同的网站。 我已经管理它登录到网站并检索它的代码。 第一个问题:通过Cookie连接到网站(setRequestProperty)

在循环HeaderFieldKeys时,没有“Set-Cookie”键,只有:Date,Server,X-Powered-By,Expires,Last-Modified,Cache-Control,Cache-Control ,Pragma,X-UA兼容,X-Frame-Options,Strict-Transport-Security,Vary,Connection,Transfer-Encoding和Content-Type。

正如你可以在我的代码中看到的,我尝试了另一个代码来检索工作的cookie。现在用cookies来导航到网站的另一个链接,但它不起作用。

package myEDKS; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintStream; 
import java.net.CookieHandler; 
import java.net.CookieManager; 
import java.net.CookiePolicy; 
import java.net.CookieStore; 
import java.net.HttpCookie; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.List; 

public class SpielHelfer 
{ 
    public static HttpCookie cookieW; 
    public static String cookieV; 
    @SuppressWarnings("deprecation") 
    public static void main(String[] args) 
    { 
     try 
     { 
      // Instantiate CookieManager; 
      // make sure to set CookiePolicy 
      CookieManager manager = new CookieManager(); 
      manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); 
      CookieHandler.setDefault(manager); 

      // get content from URLConnection; 
      // cookies are set by web site 
      URL url = new URL("https://www.w-da.net/wws/109580.php?sid=41853050108060779043427902790460"); 
      URLConnection con = url.openConnection(); 


     // activate the output 
      con.setDoOutput(true); 
      PrintStream ps = new PrintStream(con.getOutputStream()); 
      // send your parameters to your site 
      ps.print("login_login=********"); 
      ps.print("&login_password=********"); 

      // we have to get the input stream in order to actually send the request 
      con.getInputStream(); 

      // get cookies from underlying 
      // CookieStore 
      CookieStore cookieJar = manager.getCookieStore(); 
      List <HttpCookie> cookies = cookieJar.getCookies(); 
      for (HttpCookie cookie: cookies) 
      { 
       System.out.println("CookieHandler retrieved cookie: " + cookie); 
       cookieW = cookie; 
       String cookieC = cookie.getValue(); 
       int x = cookieC.length(); 
       cookieV = cookieC; 
       System.out.println("**************************************"); 
      } 
     } 
     catch(Exception e) 
     { 
      System.out.println("Unable to get cookie using CookieHandler"); 
      e.printStackTrace(); 
     } 
     System.out.println(cookieW); 

     try 
     { 
      URL url2 = new URL("https://www.w-da.net/wws/109580.php?sid=41853050108060779043427902790460"); 
      URLConnection con2 = url2.openConnection(); 
      con2.setRequestProperty("Cookie", cookieV); 
      con2.connect(); 
      BufferedReader in2 = new BufferedReader(new InputStreamReader(con2.getInputStream())); 
      String line2 = null; 
      while ((line2 = in2.readLine()) != null) 
      { 
       System.out.println(line2); 
      } 
     } 
     catch(Exception e) 
     { 
      System.out.println("Unable to set cookie using CookieHandler"); 
      e.printStackTrace(); 
     } 
    } 
} 

回答

0

我不擅长Java,但我建议尝试使用Selenium Webdriver。因为它可以帮助您以更高的方式浏览网站。它包含所有需要你的功能的很好的API。因为用这种方式写出来会很长很难。

+0

感谢您的快速回复,是的,我已阅读过有关这方面的内容,但出于灵活性和个人知识的原因,我宁愿自行编写代码。 – user3071985