dev: implement proxy creds
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Net;
|
||||
using System.Windows.Forms;
|
||||
using CarManagerV3.Forms;
|
||||
|
||||
namespace CarManagerV3
|
||||
{
|
||||
@@ -10,6 +13,8 @@ namespace CarManagerV3
|
||||
{
|
||||
|
||||
private static string _imagePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\CarManagerV3\\images";
|
||||
private static NetworkCredential myNetCred = null;
|
||||
private static bool disableImageFetch = false;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the image folder by creating it if it does not exist.
|
||||
@@ -88,17 +93,63 @@ namespace CarManagerV3
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (disableImageFetch) return;
|
||||
string url = $"https://cdn.imagin.studio/getimage?customer=hrjavascript-mastery&zoomType=fullscreen&make={car.Make}&modelFamily={car.Model}&modelYear={car.Year}&angle=front&paintDescription={car.Color}&fileType=png";
|
||||
//add Referer header to avoid 403 error
|
||||
using (var client = new System.Net.WebClient())
|
||||
{
|
||||
client.Headers.Add("Referer", "http://localhost");
|
||||
if (myNetCred != null)
|
||||
{
|
||||
client.Proxy.Credentials = myNetCred;
|
||||
}
|
||||
try
|
||||
{
|
||||
if (myNetCred == null) throw new WebException();
|
||||
client.DownloadFile(url, path);
|
||||
}
|
||||
catch
|
||||
catch (WebException ex)
|
||||
{
|
||||
// is status code 407?
|
||||
//if (ex.Response is HttpWebResponse response && response.StatusCode == HttpStatusCode.ProxyAuthenticationRequired)
|
||||
//{
|
||||
// Console.Error.WriteLine("Proxy authentication required. Prompting for credentials.");
|
||||
//}
|
||||
|
||||
if (myNetCred != null)
|
||||
{
|
||||
DialogResult disableImgDialogRetry = MessageBox.Show("Something went wrong when fetching images. Are you credentials correct? Do you want to disable Image fetching for this session or rety with different credentials?", "Invalid Credentials", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
|
||||
if(disableImgDialogRetry == DialogResult.Cancel)
|
||||
{
|
||||
disableImageFetch = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
NetCredentials netCredForm = new NetCredentials();
|
||||
DialogResult dialogRes = netCredForm.ShowDialog();
|
||||
if (dialogRes == DialogResult.OK)
|
||||
{
|
||||
NetworkCredential netcred = netCredForm.GetCredentails();
|
||||
myNetCred = netcred;
|
||||
System.Diagnostics.Debug.WriteLine($"Got credentials: {netcred.UserName}, {netcred.Password}");
|
||||
FetchImage(car);
|
||||
return;
|
||||
} else
|
||||
{
|
||||
DialogResult disableImgDialog = MessageBox.Show("Do you want to disable image fetching for this session?", "Disable Image fetching?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
||||
if(disableImgDialog == DialogResult.Yes)
|
||||
{
|
||||
disableImageFetch = true;
|
||||
} else
|
||||
{
|
||||
FetchImage(car);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
// if error, use fallback image no_image_available.png
|
||||
//System.IO.File.Copy($"{_imagePath}/no_image_available.png", path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user