how to disable system proxy setting for HttpClient in C#

hossein tavakoli 471 Reputation points
2023-07-18T09:26:40.4266667+00:00

Hi dears,

I use a V2RayN VPN for send message in Telegram Channel.

I have an WPF Windows application contains two function for send message on Telegram Channel and another App channel.

UpdateTelegramChannel();
UpdateOtherAppChannel();

When VPN is enable UpdateTelegramChannel() work correctly, but UpdateOtherAppChannel() does not work and I have to disable VPN for UpdateOtherAppCHannel().

Is there any way to dissable system proxy setting or VPN for UpdateOtherAppChannel() without disable VPN.

UpdateOtherAppChannel code :

private void UpdateOtherAppChannel()
{
	string tempImage = Path.Combine(AppContext.BaseDirectory, "tempImageEitaa.jpg");
	string url = GetAPIURL("sendFile");
	try
	{
		HttpClient client = new HttpClient();
		MultipartFormDataContent content = new MultipartFormDataContent();
		byte[] fileBytes = File.ReadAllBytes(tempImage);
		ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
		content.Add(fileContent, "file", "GeneratePicture");
		content.Add(new StringContent((userId.Trim().Length > 0) ? userId : ChannelId), "chat_id");
        content.Add(new StringContent(caption), "caption");
		HttpResponseMessage response = client.PostAsync(url, content).Result;
		string responseString = response.Content.ReadAsStringAsync().Result;
	}
	catch(exception ex)
	{throw;}
}

when VPN is disable it work correctly;

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,105 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,824 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,296 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,616 Reputation points Microsoft Vendor
    2023-07-19T06:59:38.6233333+00:00

    Hi,@hossein tavakoli. Welcome Microsoft Q&A.

    To disable the system proxy settings for the HttpClient in C# and bypass the VPN for the UpdateOtherAppChannel() method, you could try to create a custom HttpClientHandler and configure it to ignore the system proxy settings.

      private void UpdateOtherAppChannel()
            {
                string tempImage = Path.Combine(AppContext.BaseDirectory, "tempImageEitaa.jpg");
                string url = GetAPIURL("sendFile");
                try
                {
                    // Create a custom HttpClientHandler to ignore system proxy settings
                    HttpClientHandler httpClientHandler = new HttpClientHandler();
                    httpClientHandler.UseProxy = false; // Disable the system proxy settings
                    HttpClient client = new HttpClient(httpClientHandler);
    
                    MultipartFormDataContent content = new MultipartFormDataContent();
                    byte[] fileBytes = File.ReadAllBytes(tempImage);
                    ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
                    content.Add(fileContent, "file", "GeneratePicture");
                    content.Add(new StringContent((userId.Trim().Length > 0) ? userId : ChannelId), "chat_id");
                    content.Add(new StringContent(caption), "caption");
                    HttpResponseMessage response = client.PostAsync(url, content).Result;
                    string responseString = response.Content.ReadAsStringAsync().Result;
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
    
    
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jose Zero 576 Reputation points
    2023-07-18T19:29:14.1633333+00:00

    Perhaps this article can help you https://learn.microsoft.com/en-us/archive/blogs/malarch/servicepoint-bindipendpointdelegate

    With Service.BindingEndPointDelegate you can set HttpWebRequest to use an specific interface, so you can make your request using VPN interface or a Local Interface by interface IP address. Adjust article sample for your needs and give a try


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.