mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-08-16 06:31:42 +02:00
.github
.idea
Application
Browser
Configuration
Controls
Dialogs
Management
Plugins
Properties
Resources
Updates
Utils
bld
lib
TweetLib.Communication
TweetLib.Core
Application
Browser
Collections
Data
Features
Serialization
Systems
Utils
FileUtils.cs
LocaleUtils.cs
StringUtils.cs
WebUtils.cs
App.cs
Lib.cs
TweetLib.Core.csproj
TweetTest.System
TweetTest.Unit
subprocess
video
.gitattributes
.gitignore
LICENSE.md
Program.cs
README.md
Reporter.cs
TweetDuck.csproj
TweetDuck.sln
TweetDuck.sln.DotSettings
Version.cs
app.config
packages.config
65 lines
1.5 KiB
C#
65 lines
1.5 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Net;
|
|
|
|
namespace TweetLib.Core.Utils {
|
|
public static class WebUtils {
|
|
private static bool hasMicrosoftBeenBroughtTo2008Yet;
|
|
private static bool hasSystemProxyBeenEnabled;
|
|
|
|
private static void EnsureTLS12() {
|
|
if (!hasMicrosoftBeenBroughtTo2008Yet) {
|
|
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
|
|
ServicePointManager.SecurityProtocol &= ~(SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11);
|
|
hasMicrosoftBeenBroughtTo2008Yet = true;
|
|
}
|
|
}
|
|
|
|
private static bool UseSystemProxy { get; set; } = false;
|
|
|
|
public static void EnableSystemProxy() {
|
|
if (!hasSystemProxyBeenEnabled) {
|
|
UseSystemProxy = true;
|
|
hasSystemProxyBeenEnabled = true;
|
|
}
|
|
}
|
|
|
|
public static WebClient NewClient(string userAgent) {
|
|
EnsureTLS12();
|
|
|
|
WebClient client = new WebClient();
|
|
|
|
if (!UseSystemProxy) {
|
|
client.Proxy = null;
|
|
}
|
|
|
|
client.Headers[HttpRequestHeader.UserAgent] = userAgent;
|
|
return client;
|
|
}
|
|
|
|
public static AsyncCompletedEventHandler FileDownloadCallback(string file, Action? onSuccess, Action<Exception>? onFailure) {
|
|
return (sender, args) => {
|
|
if (args.Cancelled) {
|
|
TryDeleteFile(file);
|
|
}
|
|
else if (args.Error != null) {
|
|
TryDeleteFile(file);
|
|
onFailure?.Invoke(args.Error);
|
|
}
|
|
else {
|
|
onSuccess?.Invoke();
|
|
}
|
|
};
|
|
}
|
|
|
|
private static void TryDeleteFile(string file) {
|
|
try {
|
|
File.Delete(file);
|
|
} catch {
|
|
// didn't want it deleted anyways
|
|
}
|
|
}
|
|
}
|
|
}
|