简体   繁体   English

将Cookie与URLConnection一起使用(Java / Eclipse)

[英]Using cookies with a URLConnection (Java/Eclipse)

I have been working on this method for eight consecutive hours, and really need some help. 我已经连续八个小时研究此方法,真的需要一些帮助。

I am currently trying to simulate a form login and receive the cookie in return (complete). 我目前正在尝试模拟表单登录并以返回Cookie(完整)的方式接收Cookie。 However, the second step which involves using the login cookies to access other parts of the site is seemingly impossible. 但是,第二步似乎涉及使用登录cookie来访问站点的其他部分。 As a note, I am newer to Java, so bear with me :/ 注意,我是Java的新手,所以请忍受:/

public class mainClass {

  public static void main(String[] args) throws Exception{
    sources vars = new sources();   

this "vars" class contains all sources 此“ vars”类包含所有来源

    int passint = 0,userint = 0;
    String password=null,username=null,layoutte=vars.Lay;
    URLConnection oracle = new URL(vars.hComputer).openConnection();
    JPanel userPanel = new JPanel(); userPanel.setLayout(new GridLayout(2,2));  
    JLabel usernameLbl = new JLabel("Username:"); 
    JLabel passwordLbl = new JLabel("Password:");  
    JTextField usename = new JTextField(); 
    JPasswordField passwordFld = new JPasswordField();  
    userPanel.add(usernameLbl); 
    userPanel.add(usename);
    userPanel.add(passwordLbl); 
    userPanel.add(passwordFld);  
    int input = JOptionPane.showConfirmDialog(null, userPanel, "Enter your information:" ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

    char[] pass = passwordFld.getPassword();
    vars.Pass = new String(pass);
    vars.Login = usename.getText();
    password = vars.Pass;
    username = vars.Login;

successfully gets info for the cookie 成功获取cookie的信息

     CookieManager manager = new CookieManager();
     manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
     CookieHandler.setDefault(manager);


    URL url = new URL(vars.passAuth);
    URLConnection con = url.openConnection();
    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36");
    con.setDoOutput(true);


    PrintWriter wr = new PrintWriter(con.getOutputStream(), true);
    StringBuilder parameters = new StringBuilder();
    parameters.append("login=" + URLEncoder.encode(username, "UTF-8"));
    parameters.append("&");
    parameters.append("lpass=" + URLEncoder.encode(password, "UTF-8"));
    parameters.append("&");
    parameters.append("layout=" + URLEncoder.encode(layoutte, "UTF-8"));
    wr.println(parameters);
    System.out.println(parameters);
    wr.close();

send pass+username for cookie 发送通行证+ Cookie的用户名

    BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String line;
    for (int i=1; (line = con.getHeaderFieldKey(i))!=null; i++) {
        if (line.equals("Set-Cookie")) {                  
            String cookie = con.getHeaderField(i); 

         try{
             cookie = cookie.substring(0, cookie.indexOf(";"));
             String cookieName = cookie.substring(0, cookie.indexOf("="));
             String cookieValue = cookie.substring(cookie.indexOf("=") +1, cookie.length());


         }catch (Exception e){
             System.out.println("Not a cookie!");
         }



             CookieStore cookieJar =  manager.getCookieStore();
                List <HttpCookie> cookies =
                    cookieJar.getCookies();
                for (HttpCookie cookiee: cookies) {
//gets cookie here
                    oracle.setRequestProperty("Cookie", cookiee.toString());; 

        }

        }//cookies

            else if (line == null)
          break;
      }
    br.close();

at this point, the cookie is known, but i have no idea what to do with it next, as i am newer :/ the rest of this is just me trying to connect to the second url, the homepage was introduced in the first parts of the code. 在这一点上,cookie是已知的,但是我不知道下一步该怎么做,因为我是新来的:/其余的只是我试图连接到第二个URL,在第一部分介绍了主页代码。

    try{
        oracle.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
        oracle.connect();


        BufferedReader reader2  = new BufferedReader(new InputStreamReader(oracle.getInputStream(), Charset.forName("UTF-8")));

       StringBuilder sb = new StringBuilder();
       String leene;
       while ((leene = reader2.readLine()) != null) {
           sb.append(leene);
       }
       System.out.println(sb.toString());  


    }catch (IOException e){
        System.out.println(e+"BAD");
    }

}

 }//pub class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM