public object Download(IProgressHost progressHost) { ResetTimeoutState(); _finalUrl = Url; ProgressHost = progressHost; // call the URLDownloadToFile (synchronous, notifies us of progress // via calls to IBindStatusCallback) SetCookies(); int result = UrlMon.URLDownloadToFile( IntPtr.Zero, Url, FilePath, 0, this); // check for errors in the call // TODO: account for errors that we purposely generate (E_ACCESSDENIED) // HRESULT E_ABORT 0x80004004 switch (result) { case HRESULT.S_OK: // The download succeeded break; case HRESULT.E_ABORT: // The download has been cancelled //case HRESULT.E_ACCESSDENIED: // no idea if (progressHost.CancelRequested) { throw new OperationCancelledException(); } break; default: throw new COMException("Unable to download file " + Url, result); } ; return(this); }
public static string GetMIMEFromPath(string Path) { if (!File.Exists(Path)) { throw new FileNotFoundException($"\"{Path}\" not found"); } byte[] Buffer = new byte[256]; using (FileStream Stream = new FileStream(Path, FileMode.Open, FileAccess.Read)) { Stream.Read(Buffer, 0, 256); } IntPtr Pointer = Marshal.AllocHGlobal(256); try { Marshal.Copy(Buffer, 0, Pointer, 256); if (UrlMon.FindMimeFromData(null, null, Pointer, 256, null, UrlMon.FMFD.FMFD_DEFAULT, out string MIMEResult) == HRESULT.S_OK) { return(MIMEResult); } else { MimeType MIME = new MimeTypes().GetMimeTypeFromFile(Path); if (MIME != null) { return(MIME.Name); } else { return("unknown/unknown"); } } } catch (Exception ex) { LogTracer.Log(ex, $"An exception was threw in {nameof(GetMIMEFromPath)}"); return("unknown/unknown"); } finally { Marshal.FreeHGlobal(Pointer); } }
/// <summary> /// Determines whether a string is a valid Url /// </summary> /// <param name="url">The url to validate</param> /// <returns>true if the url is a valid url, otherwise false</returns> public static bool IsUrl(string url) { if (url != null && url.IndexOf("://", StringComparison.OrdinalIgnoreCase) > -1) { try { Uri uri = new Uri(url); } catch (UriFormatException) { return(false); } return(true); } else { return(false); } #if false // TODO: For some reason, IsvalidURL is always returning 1 (S_FALSE) // no matter what URL you pass into the sucker. // Handle only the base URL if (url.IndexOf("?") > -1) { url = url.Substring(0, url.IndexOf("?")); } int hResult = UrlMon.IsValidURL( IntPtr.Zero, url, 0); switch (hResult) { case HRESULT.S_OK: return(true); case HRESULT.E_INVALIDARG: Trace.Log("IsUrl returned HRESULT.E_INVALIDARG for this url: " + url); return(false); case HRESULT.S_FALSE: default: return(false); } #endif }
/// <summary> /// Initializes the Internet features that should be applied to the embedded browsers launched by this process. /// </summary> public static void InitInternetFeatures() { //Turn on Local Machine Lockdown mode. This ensures HTML accidentally executed in the //Local Machine security zone does not get privileges elevated UrlMon.CoInternetSetFeatureEnabled(FEATURE.LOCALMACHINE_LOCKDOWN, INTERNETSETFEATURE.ON_PROCESS, true); //enable the security band so it will be displayed if any embedded browser control wants to prompt for permission //Note: the security band does not work for the MSHTML editor. UrlMon.CoInternetSetFeatureEnabled(FEATURE.SECURITYBAND, INTERNETSETFEATURE.ON_PROCESS, true); //Unset locking down the Behaviors feature so that the editor's custom behaviors will load. //Local Machine lockdown mode will lockdown the behaviors feature completely for HTML executed //in the Local Machine security zone. This guarantees that editor behaviors no matter how tightly //the system has browsers locked down. //Note: this probably isn't necessary now that the editor always loads HTML in the Internet Zone. UrlMon.CoInternetSetFeatureEnabled(FEATURE.BEHAVIORS, INTERNETSETFEATURE.ON_PROCESS, false); //Turn off the annoying click sound that occurs if a browser navigation occurs in an embedded control UrlMon.CoInternetSetFeatureEnabled(FEATURE.DISABLE_NAVIGATION_SOUNDS, INTERNETSETFEATURE.ON_PROCESS, true); }