2012-07-15 52 views
6

基本上我只想从服务器获取cookie。下面是简单的代码,但我想这还不足以让像如会话ID等HttpsURLConnection和Cookies

import java.net.*; 
import java.util.*; 

    public class Cookie { 

    public static void main(String[] args) { 

     try { 
      URL url = new URL("http://www.google.com/"); 
      URLConnection conn = url.openConnection(); 
      System.out.println(con.getHeaderField("Set-Cookie")); 

     } catch (Exception e) { } 
    } 
} 

结果每一个cookie的显示,我只能得到一个饼干:

NID=61=nNSsAl4g7DfxqHE7t__ghfoCc_J-RmY7PaTNoPOd_khLdvvuqI-fnteHgnQXMzmxj_C5HdMozcGfOTt8PvePLKpbDdUlzdVZiRKwNpQIej6_T69jt9C7Oi6E4F-gWPHD; expires=Mon, 14-Jan-2013 17:16:40 GMT; path=/; domain=.google.pl; HttpOnly 

火狐获得NID和PREF,所以它的不工作。

有什么办法可以得到PREF cookie?

谁能告诉我这是正确的方式(代码如下)发送POST。

import java.io.*; 
import java.net.URL; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import javax.net.ssl.HttpsURLConnection; 

public class Cookie_Example { 

    public static void main(String[] args){ 

     try{ 
      String https_url = "https://ssl.filmweb.pl/j_login"; 
      URL url = new URL(https_url); 
      String post = "_login_redirect_url=http%253A%252F%252Fwww.filmweb.pl%252F&j_username=test.filmweb%40gmail.com&j_password=test.filmweb&_rememberMe=on&pass=zaloguj"; 

      HttpsURLConnection con = (HttpsURLConnection)url.openConnection(); 
      con.setInstanceFollowRedirects(false); 
      con.setDoInput(true); 
      con.setDoOutput(true); 
      con.setRequestMethod("GET"); 
      con.setRequestProperty("Host", "ssl.filmweb.pl"); 
      con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1"); 
      con.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
      con.setRequestProperty("Accept-Language","pl,en-us;q=0.7,en;q=0.3"); 
      con.setRequestProperty("Accept-Encoding","gzip, deflate"); 
      con.setRequestProperty("Connection","keep-alive"); 
      con.setRequestProperty("Referer","https://ssl.filmweb.pl/login"); 
      con.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
      con.setRequestProperty("Content-Length",post.length()+""); 
      DataOutput output = new DataOutputStream(new BufferedOutputStream(con.getOutputStream())); 
      output.write(post.getBytes()); 

      Map<String, List<String>> hf = con.getHeaderFields(); 

      Iterator it = hf.entrySet().iterator(); 
      while (it.hasNext()) { 
       Map.Entry pairs = (Map.Entry)it.next(); 
       System.out.println(pairs.getKey() + " = " + pairs.getValue()); 
      } 
     } catch (Exception e) { 
     } 
    } 
} 

结果为空饼干,

Set-Cookie = [_artuser_rememberMe=;Path=/;Domain=filmweb.pl;Expires=Thu, 01-Jan-1970 00:00:00 GMT] 

http://img72.imageshack.us/img72/1134/testgof.jpg

屏幕从篡改数据

这意味着这段代码也不起作用。如何获得__utma,__utmz等cookie? 任何人都可以帮助我吗?哪些书会有帮助?

+2

检查[此答案](http://stackoverflow.com/a/23865624/1223728) – Borzh 2016-04-21 11:38:46

回答

8

我认为,您的声明,con.getHeaderField("Set-Cookie")"你只能检索一个头。但也许服务器向您发送两个Cookie,分别位于两个不同的标头中。

我用另一个语句检索cookie的所有标题:

List<String> cookies = connection.getHeaderFields().get("Set-Cookie"); 

要查看您发送到服务器什么,以及服务器发送给你,我建议使用你的程序(或Web导航之间的代理)和服务器。也许Charles proxy可以帮助你。