-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from slawor/feature/httpClientBuilder
Created a static class to create HttpClient with proxy settings
- Loading branch information
Showing
4 changed files
with
42 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Moryx/Communication/Endpoints/Connector/HttpClientBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG | ||
// Licensed under the Apache License, Version 2.0 | ||
|
||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
||
namespace Moryx.Communication.Endpoints | ||
{ | ||
/// <summary> | ||
/// Helper class to get a configured HttpClient | ||
/// </summary> | ||
public static class HttpClientBuilder | ||
{ | ||
/// <summary> | ||
/// Creates a HttpClient based on the proxy config and base address | ||
/// </summary> | ||
public static HttpClient GetClient(string baseAddress, IProxyConfig proxyConfig) | ||
{ | ||
var httpClient = new HttpClient(); | ||
|
||
if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy) | ||
{ | ||
var proxy = new WebProxy | ||
{ | ||
Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"), | ||
BypassProxyOnLocal = false, | ||
UseDefaultCredentials = true | ||
}; | ||
|
||
httpClient = new HttpClient(new HttpClientHandler { Proxy = proxy }); | ||
} | ||
|
||
httpClient.BaseAddress = new Uri(baseAddress); | ||
|
||
return httpClient; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters