20/02/2013, 15:27
|
| | Fecha de Ingreso: octubre-2009
Mensajes: 47
Antigüedad: 15 años, 2 meses Puntos: 0 | |
Respuesta: Web login y descargar archivo Buenas,
Lo primero gracias por las respuestas.
Al final me he decantado por java, así es como lo he hecho;
Código:
import java.util.Date;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.net.URL;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.WebWindowListener;
import com.gargoylesoftware.htmlunit.WebWindowEvent;
import com.gargoylesoftware.htmlunit.BrowserVersion;
public class Test1
{
public static void main(String[] args) throws Exception
{
String User = "tu nombre de usuario";
String Password = "tu password";
System.out.println("El directorio temporal del sistema es " + System.getProperty("java.io.tmpdir"));
System.out.println(new Date() + "\tStarting...");
WebClient Browser = new WebClient();
Browser.getOptions().setJavaScriptEnabled(false); //Dependiendo de la pagina te veras obligado a dejarlo activo (true) para que te coga el login correctamente
System.out.println(new Date() + "\tCalling Web Site...");
LogingWeb(Browser,"https://la pagina de login/",User, Password);
AddWindowListener(Browser);
System.out.println(new Date() + "\tGetting files...");
System.out.println(new Date() + "\tGetting file...\n##################################################################");
DownloadFile(Browser,"http://www.lulu.com/items/volume_5/173000/173784/1/preview/Glossary-demo.pdf","C:\\0\\Glossary-demo.pdf");
System.out.println("##################################################################\n");
System.out.println(new Date() + "\tGetting file...\n##################################################################");
DownloadFile(Browser,"http://www.loadingsite.com/dl79b/HowToCreatePDFproducts.zip","C:\\0\\HowToCreatePDFproducts.zip");
System.out.println("##################################################################\n");
System.out.println(new Date() + "\tGetting file...\n##################################################################");
DownloadFile(Browser,"http://download.microsoft.com/download/7/2/2/7224E39E-E884-43F9-B7D2-1823623C8C51/Microsoft_Dynamics_CRM_Service_Provider_Planning_and_Deployment_Guide.docx","C:\\0\\Microsoft_Dynamics_CRM_Service_Provider_Planning_and_Deployment_Guide.docx");
System.out.println("##################################################################\n");
System.out.println(new Date() + "\tGetting file...\n##################################################################");
DownloadFile(Browser,"http://www.officehelp.biz/officehelp/DEMOS/CalendarPlan4AdvDemo.xls","C:\\0\\CalendarPlan4AdvDemo.xls");
System.out.println("##################################################################\n");
System.out.println(new Date() + "\tGetting file...\n##################################################################");
DownloadFile(Browser,"http://www.google.es//","C:\\0\\Google.html"); //lo tengo restrinjido para no bajar archivos tipo "text/html", en webWindowContentChanged lo podeis cambiar
System.out.println("##################################################################\n");
System.out.println(new Date() + "\tFiles downloaded...");
System.out.println(new Date() + "\tClosing all windows...");
Browser.closeAllWindows();
System.out.println(new Date() + "\tFinish...");
}
public static void LogingWeb(WebClient webClient, String UrlLogin, String sUser, String sPassword) throws Exception
{
//System.out.println(RefPage.getTitleText());
//System.out.println(RefPage.asXml());
HtmlPage Page = webClient.getPage(UrlLogin);
HtmlElement obj;
try{obj = Page.getElementByName("username");}catch(Exception e){obj = Page.getElementByName("USER");}
obj.click();
obj.type(sUser);
try{obj = Page.getElementByName("password");}catch(Exception e){obj = Page.getElementByName("PASSWORD");}
obj.click();
obj.type(sPassword);
try{obj = Page.getElementByName("Submit");}catch(Exception e){obj = Page.getElementByName("submit1");}
obj.click();
}
public static void StreamToFile(InputStream In, String path) throws Exception
{
OutputStream out = new FileOutputStream(new File(path));
int read = 0;
byte[] bytes = new byte[1024];
while((read = In.read(bytes)) != -1)
out.write(bytes, 0, read);
In.close();
out.flush();
out.close();
}
public static void DownloadFile(WebClient webClient, String Url, String FullPathFile)
{
String TempPath = System.getProperty("java.io.tmpdir") + "tmpFileDownload";
File afile = new File(TempPath);
afile.delete();
try{webClient.getPage(Url);}catch(Exception e){}
File bfile = new File(TempPath);
if(bfile.exists())
{
MoveAndRenameFile(TempPath, FullPathFile);
}
}
public static boolean MoveAndRenameFile(String Source, String Destination)
{
try{File afile = new File(Destination);afile.delete();}catch(Exception e){}
try{
File bfile = new File(Source);
if(bfile.renameTo(new File(Destination)))
return true;
else
return false;
}catch(Exception e){}
return false;
}
public static void AddWindowListener(final WebClient Browser)
{
Browser.addWebWindowListener(new WebWindowListener(){
public void webWindowOpened(WebWindowEvent event)
{}
public void webWindowContentChanged(WebWindowEvent event)
{
System.out.println(new Date() + "\t" + event.getWebWindow().getEnclosedPage().getUrl());
System.out.println(new Date() + "\t" + event.getWebWindow().getEnclosedPage().getWebResponse().getStatusCode());
System.out.println(new Date() + "\t" + event.getWebWindow().getEnclosedPage().getWebResponse().getStatusMessage());
System.out.println(new Date() + "\t" + event.getWebWindow().getEnclosedPage().getWebResponse().getContentType());
System.out.println(new Date() + "\t" + event.getWebWindow().getEnclosedPage().getWebResponse().getResponseHeaderValue("Content-Disposition"));
System.out.println(new Date() + "\t" + System.getProperty("java.io.tmpdir") + "tmpFileDownload");
try{
System.out.println(new Date() + "\t" + event.getWebWindow().getEnclosedPage().getWebResponse().getContentAsStream());
if(!event.getWebWindow().getEnclosedPage().getWebResponse().getContentType().equals("text/html"))
StreamToFile(event.getWebWindow().getEnclosedPage().getWebResponse().getContentAsStream(),System.getProperty("java.io.tmpdir") + "tmpFileDownload");
else
System.out.println(new Date() + "\tUPS!!!");
}
catch(Exception e){}
}
public void webWindowClosed(WebWindowEvent event)
{}
});
}
}
http://htmlunit.sourceforge.net/ http://webdesign.about.com/od/multim...ntent-type.htm |