mirror of
https://github.com/BeyondDimension/SteamTools.git
synced 2026-10-29 07:27:36 +08:00
winauth httpclient
This commit is contained in:
@@ -35,6 +35,100 @@ namespace System.Application.Services.Implementation
|
||||
/// </summary>
|
||||
static readonly bool EnableForward = true;
|
||||
|
||||
public async Task<T?> SendAsync<T>(HttpRequestMessage request, string? accept, CancellationToken cancellationToken) where T : notnull
|
||||
{
|
||||
HttpResponseMessage? response = null;
|
||||
bool notDispose = false;
|
||||
try
|
||||
{
|
||||
var requestUri = request.RequestUri.ToString();
|
||||
if (EnableForward &&
|
||||
allowUrls.Any(x => requestUri.StartsWith(x, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
try
|
||||
{
|
||||
response = await cloud_client.Forward(request,
|
||||
HttpCompletionOption.ResponseHeadersRead,
|
||||
cancellationToken);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogWarning(e, "CloudService Forward Fail.");
|
||||
response = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (response == null)
|
||||
{
|
||||
var client = CreateClient();
|
||||
response = await client.SendAsync(request,
|
||||
HttpCompletionOption.ResponseHeadersRead,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
if (response.Content != null)
|
||||
{
|
||||
var rspContentClrType = typeof(T);
|
||||
if (rspContentClrType == typeof(string))
|
||||
{
|
||||
return (T)(object)await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
else if (rspContentClrType == typeof(byte[]))
|
||||
{
|
||||
return (T)(object)await response.Content.ReadAsByteArrayAsync();
|
||||
}
|
||||
else if (rspContentClrType == typeof(Stream))
|
||||
{
|
||||
notDispose = true;
|
||||
return (T)(object)await response.Content.ReadAsStreamAsync();
|
||||
}
|
||||
var mime = response.Content.Headers.ContentType?.MediaType ?? accept;
|
||||
switch (mime)
|
||||
{
|
||||
case MediaTypeNames.JSON:
|
||||
case MediaTypeNames.XML:
|
||||
case MediaTypeNames.XML_APP:
|
||||
{
|
||||
using var stream = await response.Content
|
||||
.ReadAsStreamAsync().ConfigureAwait(false);
|
||||
using var reader = new StreamReader(stream, Encoding.UTF8);
|
||||
switch (mime)
|
||||
{
|
||||
case MediaTypeNames.JSON:
|
||||
{
|
||||
using var json = new JsonTextReader(reader);
|
||||
return jsonSerializer.Deserialize<T>(json);
|
||||
}
|
||||
case MediaTypeNames.XML:
|
||||
case MediaTypeNames.XML_APP:
|
||||
{
|
||||
var xmlSerializer = new XmlSerializer(typeof(T));
|
||||
return (T)xmlSerializer.Deserialize(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Warn(TAG, e, "GetAsync Fail.");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!notDispose)
|
||||
{
|
||||
request.Dispose();
|
||||
response?.Dispose();
|
||||
}
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
public async Task<T?> GetAsync<T>(
|
||||
string requestUri,
|
||||
string accept,
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WinAuth
|
||||
{
|
||||
partial class WinAuthSteamClient
|
||||
{
|
||||
//protected async Task<string> RequestAsync(string url, string method, NameValueCollection data, NameValueCollection headers)
|
||||
//{
|
||||
// new WinAuthSteamClient()
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ namespace WinAuth
|
||||
/// <summary>
|
||||
/// SteamClient for logging and getting/accepting/rejecting trade confirmations
|
||||
/// </summary>
|
||||
public class WinAuthSteamClient : IDisposable
|
||||
public partial class WinAuthSteamClient : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// URLs for all mobile services
|
||||
|
||||
Reference in New Issue
Block a user