使用带有URLConnection的cookie(Java / Eclipse)

时间:2015-03-13 01:00:39

标签: java eclipse oracle cookies urlconnection

我连续八个小时都在研究这种方法,真的需要一些帮助。

我目前正在尝试模拟表单登录并接收cookie(完整)。但是,第二步涉及使用登录cookie访问网站的其他部分似乎是不可能的。 作为一个注释,我比Java更新,所以请耐心等待:/

public class mainClass {

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

这" vars" class包含所有源

    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;
  

成功获取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();
  

发送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();
  

此时,cookie已知,但我不知道接下来要做什么,因为我更新:/   其余的只是我尝试连接到第二个网址,主页是在代码的第一部分介绍的。

    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

1 个答案:

答案 0 :(得分:0)

您应该使用像http://hc.apache.org/httpcomponents-client-ga/这样的库。