Skip to content

Commit c1dd0c3

Browse files
Merge pull request #2 from ShawnLaMountain/main
Changing from .NET Standard 2.1 to .NET Standard 2.0 to support more …
2 parents 1ec4ff6 + 6ef4fa7 commit c1dd0c3

File tree

10 files changed

+141
-36
lines changed

10 files changed

+141
-36
lines changed

.github/workflows/CD.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
env:
88
TITLE: "A wrapper around 'System.Net.Http.HttpClient'"
99
DESCRIPTION: "A wrapper around 'System.Net.Http.HttpClient' enhancing AutoRedirect and Cookies. Can be used in all application types."
10-
TAGS: "thunderdesign httpclient threading net"
10+
TAGS: "thunderdesign visual%2Dstudio c%2Dsharp dotnet%2Dstandard dotnet%2Dframework dotnet%2Dcore cross%2Dplatform pcl%2Dlibrary mono xamarin%2Dforms xamarin%2Dandroid xamarin%2Dios xamarin%2Dmac xamarin%2Duwp unity csharp net dotnet httpclient httpmessagehandler threading"
1111
#FILE_NAME: ex: "ThunderDesign.Net-PCL.HttpClientService"
1212
FILE_NAME: "${{ github.event.repository.name }}"
1313
#REPOSITORY_NAME: ex: "ThunderDesign.Net-PCL.HttpClientService"
@@ -30,10 +30,10 @@ jobs:
3030
- name: Checkout
3131
uses: actions/checkout@v2
3232

33-
- name: Setup .NET 3.0
33+
- name: Setup .NET 2.0
3434
uses: actions/setup-dotnet@v1
3535
with:
36-
dotnet-version: 3.0.x
36+
dotnet-version: 2.0.x
3737

3838
- name: Setup MSBuild
3939
uses: microsoft/setup-msbuild@v1.1

.github/workflows/CI.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ jobs:
2020
- name: Checkout
2121
uses: actions/checkout@v2
2222

23-
- name: Setup .NET 3.0
23+
- name: Setup .NET 2.0
2424
uses: actions/setup-dotnet@v1
2525
with:
26-
dotnet-version: 3.0.x
26+
dotnet-version: 2.0.x
2727

2828
- name: Setup MSBuild
2929
uses: microsoft/setup-msbuild@v1.1

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,38 @@
22
[![CI](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.HttpClientService/actions/workflows/CI.yml/badge.svg)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.HttpClientService/actions/workflows/CI.yml)
33
[![CD](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.HttpClientService/actions/workflows/CD.yml/badge.svg)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.HttpClientService/actions/workflows/CD.yml)
44
[![Nuget](https://img.shields.io/nuget/v/ThunderDesign.Net-PCL.HttpClientService)](https://www.nuget.org/packages/ThunderDesign.Net-PCL.HttpClientService)
5+
[![License](https://img.shields.io/github/license/ThunderDesign/ThunderDesign.Net-PCL.HttpClientService)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.HttpClientService/blob/main/LICENSE)
6+
[![Net](https://img.shields.io/badge/.net%20standard-2.0-blue)](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.HttpClientService/blob/main/README.md)
57

6-
Enhances AutoRedirect and Cookies from the standard 'System.Net.Http.HttpClient'
8+
A wrapper around 'System.Net.Http.HttpClient' enhancing AutoRedirect and Cookies. Can be used in all application types.
9+
10+
----
11+
12+
### Highlights include:
13+
14+
- DataObjects
15+
- ResponseContentData
16+
- ResponseData
17+
- EventArgs
18+
- CookieContainerChangedEventArgs
19+
- EventHandlers
20+
- CookieContainerChangedEventHandler
21+
- Extentions
22+
- CookieContainerExtention
23+
- Http
24+
- HttpClientAutoRedirect
25+
- Service
26+
- HttpClientService
27+
----
28+
29+
## Installation
30+
31+
Grab the latest [ThunderDesign.Net-PCL.HttpClientService NuGet](https://www.nuget.org/packages/ThunderDesign.Net-PCL.HttpClientService) package and install in your solution.
32+
33+
> Install-Package ThunderDesign.Net-PCL.HttpClientService
34+
35+
Use the `-version` option to specify an [older version](https://www.nuget.org/packages/ThunderDesign.Net-PCL.HttpClientService#versions-tab) to install.
36+
37+
## Please Contribute!
38+
39+
This is an open source project that welcomes contributions/suggestions/bug reports from those who use it. If you have any ideas on how to improve the library, please [post an issue here on GitHub](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.HttpClientService/issues). Please check out the [How to Contribute](https://github.com/ThunderDesign/ThunderDesign.Net-PCL.HttpClientService/blob/main/.github/CONTRIBUTING.md).

src/ThunderDesign.Net-PCL.HttpClientService/DataObjects/ResponseData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ResponseData : ThreadObject
99
{
1010
#region constructors
1111
public ResponseData() : base() { }
12-
public ResponseData(HttpResponseMessage httpResponseMessage) : base()
12+
public ResponseData(HttpResponseMessage httpResponseMessage) : base()
1313
{
1414
_IsSuccessStatusCode = httpResponseMessage.IsSuccessStatusCode;
1515
_ReasonPhrase = httpResponseMessage.ReasonPhrase;
@@ -29,7 +29,7 @@ public string ReasonPhrase
2929
get { return this.GetProperty(ref _ReasonPhrase, _Locker); }
3030
set { this.SetProperty(ref _ReasonPhrase, value, _Locker); }
3131
}
32-
32+
3333
public HttpStatusCode StatusCode
3434
{
3535
get { return this.GetProperty(ref _StatusCode, _Locker); }

src/ThunderDesign.Net-PCL.HttpClientService/Extentions/CookieContainerExtention.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ThunderDesign.Net.HttpClientService.Extentions
1010
public static class CookieContainerExtention
1111
{
1212
#region methods
13-
static public void SetCookies(this CookieContainer cookieContainer, HttpHeaders httpHeaders, string defaultDomain, CookieContainerChangedEventHandler? handler = null)
13+
static public void SetCookies(this CookieContainer cookieContainer, HttpHeaders httpHeaders, string defaultDomain, CookieContainerChangedEventHandler handler = null)
1414
{
1515
foreach (var header in httpHeaders)
1616
{
@@ -73,9 +73,11 @@ static private CookieCollection ParseCookieString(string cookieString, string de
7373
}
7474

7575
DateTime expiresDateTime;
76-
if (expiresString != null)
76+
if (!string.IsNullOrEmpty(expiresString))
7777
{
78-
expiresDateTime = DateTime.Parse(expiresString);
78+
if (!DateTime.TryParse(expiresString, out expiresDateTime))
79+
expiresDateTime = DateTime.MinValue;
80+
//expiresDateTime = DateTime.Parse(expiresString);
7981
}
8082
else
8183
{

src/ThunderDesign.Net-PCL.HttpClientService/Http/HttpClientAutoRedirect.cs

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Net;
44
using System.Net.Http;
5+
using System.Reflection;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using ThunderDesign.Net.HttpClientService.EventHandlers;
@@ -23,13 +24,62 @@ public HttpClientAutoRedirect(HttpMessageHandler handler) : this(handler, true)
2324

2425
public HttpClientAutoRedirect(HttpMessageHandler handler, bool disposeHandler) : base(handler, disposeHandler)
2526
{
26-
if (handler is HttpClientHandler)
27-
_HttpClientHandler = handler as HttpClientHandler;
27+
if (handler == null)
28+
return;
29+
30+
//When you create your custom HttpMessageHandler include ICustomHttpMessageHandler to quickly read property values
31+
if (handler is ICustomHttpMessageHandler customHttpMessageHandler)
32+
{
33+
this.CookieContainer = customHttpMessageHandler.CookieContainer ?? new CookieContainer();
34+
this.UseCookies = customHttpMessageHandler.UseCookies;
35+
this.AllowAutoRedirect = customHttpMessageHandler.AllowAutoRedirect;
36+
37+
//need to turn off AllowAutoRedirect since we are using our own
38+
if (customHttpMessageHandler.AllowAutoRedirect)
39+
customHttpMessageHandler.AllowAutoRedirect = false;
40+
}
41+
//android will be HttpClientHandler, but ios won't. Recommend creating custom inherited class that uses ICustomHttpMessageHandler
42+
else if (handler is HttpClientHandler httpClientHandler)
43+
{
44+
this.CookieContainer = httpClientHandler.CookieContainer ?? new CookieContainer();
45+
this.UseCookies = httpClientHandler.UseCookies;
46+
this.AllowAutoRedirect = httpClientHandler.AllowAutoRedirect;
47+
48+
//need to turn off AllowAutoRedirect since we are using our own
49+
if (httpClientHandler.AllowAutoRedirect)
50+
httpClientHandler.AllowAutoRedirect = false;
51+
}
52+
//using reflection to read property values
53+
else
54+
{
55+
PropertyInfo propertyInfo;
56+
propertyInfo = handler.GetType().GetProperty(nameof(ICustomHttpMessageHandler.CookieContainer));
57+
if (propertyInfo?.PropertyType == typeof(CookieContainer))
58+
this.CookieContainer = (CookieContainer)propertyInfo.GetValue(handler) ?? new CookieContainer();
59+
60+
propertyInfo = handler.GetType().GetProperty(nameof(ICustomHttpMessageHandler.UseCookies));
61+
if (propertyInfo?.PropertyType == typeof(bool))
62+
this.UseCookies = (bool)propertyInfo.GetValue(handler);
63+
64+
propertyInfo = handler.GetType().GetProperty(nameof(ICustomHttpMessageHandler.AllowAutoRedirect));
65+
if (propertyInfo?.PropertyType == typeof(bool))
66+
this.AllowAutoRedirect = (bool)propertyInfo.GetValue(handler);
67+
68+
if (this.AllowAutoRedirect)
69+
//need to turn off AllowAutoRedirect since we are using our own
70+
handler.GetType().GetProperty(nameof(ICustomHttpMessageHandler.AllowAutoRedirect)).SetValue(handler, false);
71+
}
2872
}
2973
#endregion
3074

3175
#region event handlers
32-
public event CookieContainerChangedEventHandler? CookieContainerChangedEvent;
76+
public event CookieContainerChangedEventHandler CookieContainerChangedEvent;
77+
#endregion
78+
79+
#region properties
80+
public bool UseCookies { get; private set; }
81+
public bool AllowAutoRedirect { get; private set; }
82+
public CookieContainer CookieContainer { get; private set; }
3383
#endregion
3484

3585
#region methods
@@ -54,23 +104,25 @@ public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
54104
task.Wait(cancellationToken);
55105
HttpResponseMessage responseMessage = task.Result;
56106

57-
if ((_HttpClientHandler?.UseCookies ?? false) && responseMessage.Headers.Contains("set-cookie"))
107+
if (this.UseCookies && responseMessage.Headers.Contains("set-cookie"))
58108
{
59-
_HttpClientHandler?.CookieContainer.SetCookies(responseMessage.Headers, responseMessage.RequestMessage.RequestUri.Host, CookieContainerChangedEvent);
109+
this.CookieContainer?.SetCookies(responseMessage.Headers, responseMessage.RequestMessage.RequestUri.Host, CookieContainerChangedEvent);
60110
}
61111

62-
if ((_HttpClientHandler?.AllowAutoRedirect ?? false) && responseMessage.StatusCode == HttpStatusCode.Redirect && responseMessage.Headers.Contains("location"))
112+
if (this.AllowAutoRedirect && Enumerable.Range(300, 399).Contains((int)responseMessage.StatusCode) && responseMessage.Headers.Contains("location"))
63113
{
64114
if (responseMessage.Headers.TryGetValues("location", out var location))
65115
{
66-
using HttpRequestMessage requestMessage = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri(location.Single()) };
67-
try
68-
{
69-
return SendAsync(requestMessage, cancellationToken);
70-
}
71-
finally
116+
using (HttpRequestMessage requestMessage = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri(location.Single()) })
72117
{
73-
responseMessage.Dispose();
118+
try
119+
{
120+
return SendAsync(requestMessage, cancellationToken);
121+
}
122+
finally
123+
{
124+
responseMessage.Dispose();
125+
}
74126
}
75127
}
76128
return task;
@@ -82,7 +134,6 @@ public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
82134

83135
#region variables
84136
private const HttpCompletionOption defaultCompletionOption = HttpCompletionOption.ResponseContentRead;
85-
private readonly HttpClientHandler? _HttpClientHandler;
86137
#endregion
87138
}
88139
}

src/ThunderDesign.Net-PCL.HttpClientService/HttpClientService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using ThunderDesign.Net.HttpClientService.Http;
33
using ThunderDesign.Net.Threading.Objects;
44

5-
namespace HttpClientService.Core
5+
namespace ThunderDesign.Net.HttpClientService
66
{
77
public class HttpClientService : ThreadObject
88
{
@@ -11,15 +11,15 @@ public HttpClientService() : this(DefaultHttpClientHandler)
1111
{
1212
}
1313

14-
public HttpClientService(HttpMessageHandler? httpClientHandler)
14+
public HttpClientService(HttpMessageHandler httpClientHandler)
1515
{
1616
HttpClient = httpClientHandler != null ?
1717
new HttpClientAutoRedirect(httpClientHandler) : new HttpClientAutoRedirect();
1818
}
1919
#endregion
2020

2121
#region properties
22-
public static HttpMessageHandler? DefaultHttpClientHandler
22+
public static HttpMessageHandler DefaultHttpClientHandler
2323
{
2424
get { lock (_Locker) { return _DefaultHttpClientHandler; } }
2525
set { lock (_Locker) { _DefaultHttpClientHandler = value; } }
@@ -31,7 +31,7 @@ public static HttpClientService Instance
3131
{
3232
lock (_Locker)
3333
{
34-
return _Instance ??= new HttpClientService();
34+
return _Instance ?? (_Instance = new HttpClientService());
3535
}
3636
}
3737
}
@@ -44,8 +44,8 @@ public HttpClientAutoRedirect HttpClient
4444
#endregion
4545

4646
#region variables
47-
private static HttpClientService? _Instance = null;
48-
private static HttpMessageHandler? _DefaultHttpClientHandler = null;
47+
private static HttpClientService _Instance = null;
48+
private static HttpMessageHandler _DefaultHttpClientHandler = null;
4949
#endregion
5050
}
5151
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Net;
2+
3+
namespace ThunderDesign.Net.HttpClientService.Interfaces
4+
{
5+
public interface ICustomHttpMessageHandler
6+
{
7+
#region properties
8+
bool UseCookies { get; set; }
9+
bool AllowAutoRedirect { get; set; }
10+
CookieContainer CookieContainer { get; set; }
11+
#endregion
12+
}
13+
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ThunderDesign.Net.HttpClientService.EventHandlers;
1+
using System.Net;
2+
using ThunderDesign.Net.HttpClientService.EventHandlers;
23

34
namespace ThunderDesign.Net.HttpClientService.Interfaces
45
{
@@ -7,5 +8,11 @@ public interface IHttpClientAutoRedirect
78
#region event handlers
89
event CookieContainerChangedEventHandler CookieContainerChangedEvent;
910
#endregion
11+
12+
#region properties
13+
bool UseCookies { get; }
14+
bool AllowAutoRedirect { get; }
15+
CookieContainer CookieContainer { get; }
16+
#endregion
1017
}
1118
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>ThunderDesign.Net_PCL.HttpClientService</RootNamespace>
6-
<Nullable>enable</Nullable>
76
</PropertyGroup>
87

98
<ItemGroup>
10-
<PackageReference Include="ThunderDesign.Net-PCL.Threading" Version="1.0.0" />
9+
<PackageReference Include="ThunderDesign.Net-PCL.Threading" Version="1.0.5" />
1110
</ItemGroup>
1211

1312
</Project>

0 commit comments

Comments
 (0)