Download file webclient
www.cronistalascolonias.com.ar › download-files. TAGS web download webclient /blob/master/Actions/Networking/Invoke-www.cronistalascolonias.com.ar1 Download files from the internet through PowerShell. #>. Using www.cronistalascolonias.com.aradFile() is a very efficient way to download a file and save it somewhere to be processed. Notice that a cookie is being sent to the server.
usingSystem; www.cronistalascolonias.com.ar; www.cronistalascolonias.com.ar; publicstaticclassFileDownloader { privateconststringGOOGLE_DRIVE_DOMAIN="www.cronistalascolonias.com.ar"; privateconststringGOOGLE_DRIVE_DOMAIN2="www.cronistalascolonias.com.ar"; // Normal example: www.cronistalascolonias.com.aradFileFromURLToPath( "www.cronistalascolonias.com.ar", @"C:\www.cronistalascolonias.com.ar" ); // Drive example: www.cronistalascolonias.com.aradFileFromURLToPath( "www.cronistalascolonias.com.ar?usp=sharing", @"C:\www.cronistalascolonias.com.ar" ); publicstaticFileInfoDownloadFileFromURLToPath( stringurl, stringpath ) { if( www.cronistalascolonias.com.arWith( GOOGLE_DRIVE_DOMAIN ) ||www.cronistalascolonias.com.arWith( GOOGLE_DRIVE_DOMAIN2 ) ) returnDownloadGoogleDriveFileFromURLToPath( url, path ); else returnDownloadFileFromURLToPath( url, path, null ); } privatestaticFileInfoDownloadFileFromURLToPath( stringurl, stringpath, WebClientwebClient ) { try { if( webClient==null ) { using( webClient=newWebClient() ) { www.cronistalascolonias.com.aradFile( url, path ); returnnewFileInfo( path ); } } else { www.cronistalascolonias.com.aradFile( url, path ); returnnewFileInfo( path ); } } catch( WebException ) { returnnull; } } // Downloading large files from Google Drive prompts a warning screen and // requires manual confirmation. Consider that case and try to confirm the download automatically // if warning prompt occurs privatestaticFileInfoDownloadGoogleDriveFileFromURLToPath( stringurl, stringpath ) { // You can comment the statement below if the provided url is guaranteed to be in the following format: // www.cronistalascolonias.com.ar?id=FILEID&export=download url=GetGoogleDriveDownloadLinkFromUrl( url ); using( CookieAwareWebClientwebClient=newCookieAwareWebClient() ) { FileInfodownloadedFile; // Sometimes Drive returns an NID cookie instead of a download_warning cookie at first attempt, // but works in the second attempt for( inti=0; i<2; i++ ) { downloadedFile=DownloadFileFromURLToPath( url, path, webClient ); if( downloadedFile==null ) returnnull; // Confirmation page is around 50KB, shouldn't be larger than 60KB if( www.cronistalascolonias.com.ar> ) returndownloadedFile; // Downloaded file might be the confirmation page, check it stringcontent; using( varreader=www.cronistalascolonias.com.arxt() ) { // Confirmation page starts with <!DOCTYPE html>, which can be preceeded by a newline char[] header=newchar[20]; intreadCount=www.cronistalascolonias.com.arock( header, 0, 20 ); if( readCount<20||!( newstring( header ).Contains( "<!DOCTYPE html>" ) ) ) returndownloadedFile; content=www.cronistalascolonias.com.arEnd(); } intlinkIndex=www.cronistalascolonias.com.ardexOf( "href=\"/uc?" ); if( linkIndex<0 ) returndownloadedFile; linkIndex+=6; intlinkEnd=www.cronistalascolonias.com.arf( '"', linkIndex ); if( linkEnd<0 ) returndownloadedFile; url="www.cronistalascolonias.com.ar"+www.cronistalascolonias.com.aring( linkIndex, linkEnd-linkIndex ).Replace( "&", "&" ); } downloadedFile=DownloadFileFromURLToPath( url, path, webClient ); returndownloadedFile; } } // Handles 3 kinds of links (they can be preceeded by https://): // - www.cronistalascolonias.com.ar?id=FILEID // - www.cronistalascolonias.com.ar?usp=sharing // - www.cronistalascolonias.com.ar?id=FILEID&export=download publicstaticstringGetGoogleDriveDownloadLinkFromUrl( stringurl ) { intindex=www.cronistalascolonias.com.arf( "id=" ); intclosingIndex; if( index>0 ) { index+=3; closingIndex=www.cronistalascolonias.com.arf( '&', index ); if( closingIndex<0 ) closingIndex=www.cronistalascolonias.com.ar; } else { index=www.cronistalascolonias.com.arf( "file/d/" ); if( index<0 ) // url is not in any of the supported forms www.cronistalascolonias.com.ar; index+=7; closingIndex=www.cronistalascolonias.com.arf( '/', index ); if( closingIndex<0 ) { closingIndex=www.cronistalascolonias.com.arf( '?', index ); if( closingIndex<0 ) closingIndex=www.cronistalascolonias.com.ar; } } www.cronistalascolonias.com.ar( "www.cronistalascolonias.com.ar?id={0}&export=download", www.cronistalascolonias.com.aring( index, closingIndex-index ) ); } } // Web client used for Google Drive publicclassCookieAwareWebClient : WebClient { privateclassCookieContainer { Dictionary<string, string> _cookies; publicstringthis[Uriurl] { get { stringcookie; if( _www.cronistalascolonias.com.arValue( www.cronistalascolonias.com.ar, outcookie ) ) returncookie; returnnull; } set { _cookies[www.cronistalascolonias.com.ar] =value; } } publicCookieContainer() { _cookies=newDictionary<string, string>(); } } privateCookieContainercookies; publicCookieAwareWebClient() : base() { cookies=newCookieContainer(); } protectedoverrideWebRequestGetWebRequest( Uriaddress ) { WebRequestrequest=www.cronistalascolonias.com.arRequest( address ); if( requestisHttpWebRequest ) { stringcookie=cookies[address]; if( cookie!=null ) ( (HttpWebRequest) request )www.cronistalascolonias.com.ar( "cookie", cookie ); } returnrequest; } protectedoverrideWebResponseGetWebResponse( WebRequestrequest, IAsyncResultresult ) { WebResponseresponse=www.cronistalascolonias.com.arResponse( request, result ); string[] cookies=www.cronistalascolonias.com.arues( "Set-Cookie" ); if( cookies!=null&&www.cronistalascolonias.com.ar>0 ) { stringcookie=""; foreach( stringcincookies ) cookie+=c; www.cronistalascolonias.com.ars[www.cronistalascolonias.com.arseUri] =cookie; } returnresponse; } protectedoverrideWebResponseGetWebResponse( WebRequestrequest ) { WebResponseresponse=www.cronistalascolonias.com.arResponse( request ); Источник: www.cronistalascolonias.com.ar
-
-
-