Could someone help me. I have a little problem of setting the boolean property to true.
Here I created a class for my proxy so that I can set and call from my winform to my httpclient class.
class MyProxy
{
public static bool UseProxy { get; set; }
public static string ProxyHost { get; set; } = string.Empty;
public static string ProxyPort { get; set; } = string.Empty;
}
and here's the optional if I want to use proxy or not
private static HttpClientHandler handler = new HttpClientHandler
{
UseDefaultCredentials = false,
PreAuthenticate = true,
Proxy = new WebProxy($"http://{MyProxy.ProxyHost}:{MyProxy.ProxyPort}", false),
UseProxy = MyProxy.UseProxy, // i want to change this if I want to use the proxy or not
};
So in my main form, I have a checkbox whether use proxy or not
private void TEST()
{
bool isCheck = cbUseProxy.Checked;
//so here I'll check first if isCheck is true
MyProxy.UseProxy = isCheck;
//here I call my httpclient class
}
The problem is, if I set first or check first the checkbox (which is useProxy) the Webproxy is getting the value. and I uncheck (which is not to use proxy) the webproxy still getting the first value (which is true).
I want to change from true to false and false to true the value of UseProxy (from WebProxy).
UPDATED FINAL SOLUTION: (thanks to @Milney for the suggestion. I think this is the solution)
private static HttpClientHandler handler = new HttpClientHandler
{
PreAuthenticate = true,
UseDefaultCredentials = false,
Proxy = null,
UseProxy = false,
};
private static HttpClientHandler handlerWithProxy = new HttpClientHandler
{
PreAuthenticate = true,
UseDefaultCredentials = false,
Proxy = new WebProxy($"http://{MyProxy.ProxyHost}:{MyProxy.ProxyPort}", false),
UseProxy = true,
};
private static readonly HttpClient client = new HttpClient(handler);
private static readonly HttpClient clientWithProxy = new HttpClient(handlerWithProxy);
and then on my ResponseMessage
private static async Task<JsonDocument> ResponseMessage(HttpRequestMessage request, CancellationToken token)
{
HttpCompletionOption option = HttpCompletionOption.ResponseHeadersRead;
bool useProxy = MyProxy.UseProxy;
using (var response = useProxy ? await clientWithProxy.SendAsync(request, option, token).ConfigureAwait(false)
: await client.SendAsync(request, option, token).ConfigureAwait(false))
{
token.ThrowIfCancellationRequested();
using (var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
var json = await ParseJsonFromStream(contentStream);
if (!response.IsSuccessStatusCode)
{
if (json != null)
{
throw new InvalidDataException($"Error occured: {ParseError(uri, json)}");
}
}
return json;
}
}
}
At the time you create the HttpClientHandler, the MyProxy.UseProxy has its default value. If you change it later on it won't automatically 'flow' through to the client handler properties. You'll need to recreate your HttpClientHandler, or atleast reset the property on it AFTER you actually get the value you want to use...
private void TEST()
{
bool isCheck = cbUseProxy.Checked;
// You have to set the property on the handler
// It won't automatically refresh from your other class
handler.UseProxy = isCheck;
//here I call my httpclient class
}