Is it possible to ignore any vpn/proxy profiles (such as those created by 1.1.1.1 app etc) and use pure WiFi connection (if it's available) in xamarin forms app ?
I am working on an api which is not accessible outside the network, any sort of proxy or vpn can interfere with the request.
My HttpClient and HttpClientHandler code
protected HttpClientHandler ClientHandler {
get {
if(_clientHandler == null) {
_clientHandler = new HttpClientHandler() {
AllowAutoRedirect = true,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
ClientCertificateOptions = ClientCertificateOption.Automatic,
CookieContainer = Cookies,
UseCookies = true,
UseProxy = false,
SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11
};
_clientHandler.ServerCertificateCustomValidationCallback = CertificateValidationCallback;
}
return _clientHandler;
}
}
protected HttpClient Client {
get {
if(_client == null) {
_client = new HttpClient(ClientHandler, false);
_client.Timeout = TimeSpan.FromSeconds(TIMEOUT);
_client.DefaultRequestHeaders.UserAgent.ParseAdd($"{SharedInfo.ProjectName}/{SharedInfo.Version}");
}
return _client;
}
}
i currently have code to force either wifi or mobile data connection (searched a lot to find this)
but it isn't what i require at the moment.
public class ConnectionManager : IConnectionManager {
Context _context => Platform.AppContext;
ConnectivityManager _manager => (ConnectivityManager) _context.GetSystemService(Context.ConnectivityService);
public void ForceMobileData() {
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.AddTransportType(TransportType.Cellular);
_manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack());
}
public void ForceWifi() {
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.AddTransportType(TransportType.Wifi);
request.AddCapability(NetCapability.NotVpn);
_manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack());
}
/// <summary>
/// Custom network available call back.
/// </summary>
public class CustomNetworkAvailableCallBack : ConnectivityManager.NetworkCallback {
Context _context => Platform.AppContext;
ConnectivityManager _manager => (ConnectivityManager) _context.GetSystemService(Context.ConnectivityService);
public override void OnAvailable(Network network) {
//ConnectivityManager.SetProcessDefaultNetwork(network); //deprecated (but works even in Android P)
_manager.BindProcessToNetwork(network); //this works in Android P
}
}
}
question from:
https://stackoverflow.com/questions/65651474/ignore-vpn-proxy-profiles-in-xamarin-forms-app 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…