Ver Mensaje Individual
  #2 (permalink)  
Antiguo 27/12/2011, 02:32
kamita
 
Fecha de Ingreso: agosto-2008
Mensajes: 89
Antigüedad: 16 años, 6 meses
Puntos: 1
Exclamación Respuesta: Enviar formulario con POST

Código:
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(nia), "UTF-8");
        data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
        data += "&" + URLEncoder.encode("submit", "UTF-8") + "=" + URLEncoder.encode("Entrar", "UTF-8");
        data += "&" + URLEncoder.encode("testcookies", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8");
        data += "&" + URLEncoder.encode("nosso", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");

        //To handle cookies and therefore get redirected to the site
        //CookieHandler.setDefault(new CookieManager());
        
        URL url = new URL("WEB DEL SITE https://www.loquesea.com/index.php");
        HttpURLConnection con =  (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
        wr.write(data);
        wr.flush();

        BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String aux;
        String line = "";
        while ((aux = rd.readLine()) != null) {
          line += aux;
        }
        wr.close();
        rd.close();       
     
        //We parse Line and find whether password is correct or not
        if(line.indexOf("<div class=\"loginerrors\">") != -1){
            return false;
        }else{
            return true;
        }