He intentado esto pero se logea y no coge las cookies intentaré con httpclient.
Código:
package prueba_mega;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author oem
*/
public class Prueba_mega {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
try {
String urlParameters =
"login=" + URLEncoder.encode("1", "UTF-8") +
"&redir=" + URLEncoder.encode("1", "UTF-8") +
"&username=" + URLEncoder.encode("user", "UTF-8") +
"&password=" + URLEncoder.encode("password", "UTF-8");
String dir = "http://www.megaupload.com/";
String salida = excutePost(dir,urlParameters);
System.out.println("\n\n\n" + salida);
String sFichero = "fichero.html";
File fichero = new File(sFichero);
BufferedWriter bw = new BufferedWriter(new FileWriter(sFichero));
bw.write(salida);
bw.flush();
bw.close();
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Prueba_mega.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static String excutePost(String targetURL, String urlParameters)
{
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.0; de-DE; rv:1.7.5) Gecko/20041122 Firefox/1.0");
connection.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
connection.setRequestProperty("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
connection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
connection.setRequestProperty("Keep-Alive", "300");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Referer", "https://www.megaupload.com/?c=login");
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
for (int i=0; ; i++)
{
String headerName = connection.getHeaderFieldKey(i);
String headerValue = connection.getHeaderField(i);
if (headerName == null && headerValue == null)
{
// No more headers
break;
}
if ("Set-Cookie".equalsIgnoreCase(headerName))
{
// Parse cookie
String[] fields = headerValue.split(";\\s*");
String cookieValue = fields[0];
String cookieValueName = "";
String cookieValueValue = "";
if(cookieValue.indexOf("=") != -1)
{
String[] sessionId = cookieValue.split("=");
cookieValueName = sessionId[0];
cookieValueValue = sessionId[1];
}
else
{
cookieValueName = "cookieValue";
cookieValueValue = cookieValue;
}
System.out.println(cookieValueName + "=" + cookieValueValue);
}
}
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
si encuentran el fallo por favor avísenme.
Muchísimas gracias.
Marcadores