2020-02-14 22:23:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-02-26 04:00:28 +00:00
|
|
|
|
using System.IO;
|
2020-02-14 22:23:27 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common.Http
|
|
|
|
|
{
|
|
|
|
|
public class Client
|
|
|
|
|
{
|
2020-02-14 22:50:14 +00:00
|
|
|
|
public List<(string, string)> Headers = new List<(string, string)>();
|
2020-02-14 22:23:27 +00:00
|
|
|
|
public List<Cookie> Cookies = new List<Cookie>();
|
2020-02-26 04:00:28 +00:00
|
|
|
|
public async Task<HttpResponseMessage> GetAsync(string url, HttpCompletionOption responseHeadersRead = HttpCompletionOption.ResponseHeadersRead)
|
2020-02-14 22:23:27 +00:00
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
|
|
|
|
return await SendAsync(request, responseHeadersRead);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 04:00:28 +00:00
|
|
|
|
|
|
|
|
|
public async Task<HttpResponseMessage> PostAsync(string url, HttpContent content, HttpCompletionOption responseHeadersRead = HttpCompletionOption.ResponseHeadersRead)
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, url) {Content = content};
|
|
|
|
|
return await SendAsync(request, responseHeadersRead);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<HttpResponseMessage> PutAsync(string url, HttpContent content, HttpCompletionOption responseHeadersRead = HttpCompletionOption.ResponseHeadersRead)
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Put, url) {Content = content};
|
|
|
|
|
return await SendAsync(request, responseHeadersRead);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 22:23:27 +00:00
|
|
|
|
public async Task<string> GetStringAsync(string url)
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
|
|
|
|
return await SendStringAsync(request);
|
|
|
|
|
}
|
2020-03-02 23:16:15 +00:00
|
|
|
|
|
|
|
|
|
public async Task<string> DeleteStringAsync(string url)
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Delete, url);
|
|
|
|
|
return await SendStringAsync(request);
|
|
|
|
|
}
|
2020-02-14 22:23:27 +00:00
|
|
|
|
|
|
|
|
|
private async Task<string> SendStringAsync(HttpRequestMessage request)
|
|
|
|
|
{
|
2020-02-26 05:05:33 +00:00
|
|
|
|
using var result = await SendAsync(request);
|
2020-02-14 22:23:27 +00:00
|
|
|
|
return await result.Content.ReadAsStringAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 04:00:28 +00:00
|
|
|
|
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage msg, HttpCompletionOption responseHeadersRead = HttpCompletionOption.ResponseHeadersRead)
|
2020-02-14 22:23:27 +00:00
|
|
|
|
{
|
2020-02-28 02:26:58 +00:00
|
|
|
|
foreach (var (k, v) in Headers)
|
|
|
|
|
msg.Headers.Add(k, v);
|
|
|
|
|
if (Cookies.Count > 0)
|
|
|
|
|
Cookies.ForEach(c => ClientFactory.Cookies.Add(c));
|
2020-02-14 22:23:27 +00:00
|
|
|
|
int retries = 0;
|
|
|
|
|
TOP:
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var response = await ClientFactory.Client.SendAsync(msg, responseHeadersRead);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
if (retries > Consts.MaxHTTPRetries) throw;
|
|
|
|
|
|
|
|
|
|
retries++;
|
|
|
|
|
Utils.Log($"Http Connect error to {msg.RequestUri} retry {retries}");
|
|
|
|
|
await Task.Delay(100 * retries);
|
2020-02-15 23:28:20 +00:00
|
|
|
|
msg = CloneMessage(msg);
|
2020-02-14 22:23:27 +00:00
|
|
|
|
goto TOP;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-02-15 23:28:20 +00:00
|
|
|
|
|
|
|
|
|
private HttpRequestMessage CloneMessage(HttpRequestMessage msg)
|
|
|
|
|
{
|
|
|
|
|
var new_message = new HttpRequestMessage(msg.Method, msg.RequestUri);
|
|
|
|
|
foreach (var header in msg.Headers)
|
|
|
|
|
new_message.Headers.Add(header.Key, header.Value);
|
|
|
|
|
new_message.Content = msg.Content;
|
|
|
|
|
return new_message;
|
|
|
|
|
|
|
|
|
|
}
|
2020-02-14 22:23:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|