mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-24 06:56:27 +00:00
Switched from WebClient to HTTPClient
This commit is contained in:
parent
8acea4f36e
commit
25a21fd342
@ -38,7 +38,7 @@ namespace SpotifyAPI.Example
|
||||
}
|
||||
|
||||
authButton.Enabled = false;
|
||||
_profile = _spotify.GetPrivateProfile();
|
||||
_profile = await _spotify.GetPrivateProfileAsync();
|
||||
|
||||
_savedTracks = GetSavedTracks();
|
||||
savedTracksCountLabel.Text = _savedTracks.Count.ToString();
|
||||
|
@ -4,6 +4,7 @@ using NUnit.Framework;
|
||||
using SpotifyAPI.Web;
|
||||
using SpotifyAPI.Web.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
@ -50,23 +51,29 @@ namespace SpotifyAPI.Tests
|
||||
public void ShouldGetPrivateProfile_WithAuth()
|
||||
{
|
||||
PrivateProfile profile = GetFixture<PrivateProfile>("private-user.json");
|
||||
_mock.Setup(client => client.DownloadJson<PrivateProfile>(It.IsAny<string>()))
|
||||
_mock.Setup(client => client.DownloadJson<PrivateProfile>(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
|
||||
.Returns(new Tuple<ResponseInfo, PrivateProfile>(ResponseInfo.Empty, profile));
|
||||
|
||||
_spotify.UseAuth = true;
|
||||
Assert.AreEqual(profile, _spotify.GetPrivateProfile());
|
||||
_mock.Verify(client => client.DownloadJson<PrivateProfile>(It.Is<string>(str => ContainsValues(str, "/me"))), Times.Exactly(1));
|
||||
_mock.Verify(client => client.DownloadJson<PrivateProfile>(
|
||||
It.Is<string>(str => ContainsValues(str, "/me")),
|
||||
It.IsNotNull<Dictionary<string, string>>()), Times.Exactly(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldGetPublicProfile()
|
||||
{
|
||||
PublicProfile profile = GetFixture<PublicProfile>("public-user.json");
|
||||
_mock.Setup(client => client.DownloadJson<PublicProfile>(It.IsAny<string>()))
|
||||
_mock.Setup(client => client.DownloadJson<PublicProfile>(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
|
||||
.Returns(new Tuple<ResponseInfo, PublicProfile>(ResponseInfo.Empty, profile));
|
||||
|
||||
|
||||
_spotify.UseAuth = false;
|
||||
Assert.AreEqual(profile, _spotify.GetPublicProfile("wizzler"));
|
||||
_mock.Verify(client => client.DownloadJson<PublicProfile>(It.Is<string>(str => ContainsValues(str, "/users/wizzler"))), Times.Exactly(1));
|
||||
_mock.Verify(client => client.DownloadJson<PublicProfile>(
|
||||
It.Is<string>(str => ContainsValues(str, "/users/wizzler")),
|
||||
It.Is<Dictionary<string, string>>(headers => headers.Count == 0)), Times.Exactly(1));
|
||||
}
|
||||
|
||||
//Will add more tests once I decided if this is worth the effort (propably not?)
|
||||
|
@ -45,6 +45,7 @@
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
|
@ -1,6 +1,7 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using SpotifyAPI.Web.Models;
|
||||
|
||||
@ -15,28 +16,28 @@ namespace SpotifyAPI.Web
|
||||
/// </summary>
|
||||
/// <param name="url">An URL</param>
|
||||
/// <returns></returns>
|
||||
Tuple<ResponseInfo, string> Download(string url);
|
||||
Tuple<ResponseInfo, string> Download(string url, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Downloads data async from an URL and returns it
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
Task<Tuple<ResponseInfo, string>> DownloadAsync(string url);
|
||||
Task<Tuple<ResponseInfo, string>> DownloadAsync(string url, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Downloads data from an URL and returns it
|
||||
/// </summary>
|
||||
/// <param name="url">An URL</param>
|
||||
/// <returns></returns>
|
||||
Tuple<ResponseInfo, byte[]> DownloadRaw(string url);
|
||||
Tuple<ResponseInfo, byte[]> DownloadRaw(string url, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Downloads data async from an URL and returns it
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url);
|
||||
Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Downloads data from an URL and converts it to an object
|
||||
@ -44,7 +45,7 @@ namespace SpotifyAPI.Web
|
||||
/// <typeparam name="T">The Type which the object gets converted to</typeparam>
|
||||
/// <param name="url">An URL</param>
|
||||
/// <returns></returns>
|
||||
Tuple<ResponseInfo, T> DownloadJson<T>(string url);
|
||||
Tuple<ResponseInfo, T> DownloadJson<T>(string url, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Downloads data async from an URL and converts it to an object
|
||||
@ -52,7 +53,7 @@ namespace SpotifyAPI.Web
|
||||
/// <typeparam name="T">The Type which the object gets converted to</typeparam>
|
||||
/// <param name="url">An URL</param>
|
||||
/// <returns></returns>
|
||||
Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url);
|
||||
Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Uploads data from an URL and returns the response
|
||||
@ -61,7 +62,7 @@ namespace SpotifyAPI.Web
|
||||
/// <param name="body">The Body-Data (most likely a JSON String)</param>
|
||||
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
|
||||
/// <returns></returns>
|
||||
Tuple<ResponseInfo, string> Upload(string url, string body, string method);
|
||||
Tuple<ResponseInfo, string> Upload(string url, string body, string method, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Uploads data async from an URL and returns the response
|
||||
@ -70,7 +71,7 @@ namespace SpotifyAPI.Web
|
||||
/// <param name="body">The Body-Data (most likely a JSON String)</param>
|
||||
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
|
||||
/// <returns></returns>
|
||||
Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string body, string method);
|
||||
Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string body, string method, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Uploads data from an URL and returns the response
|
||||
@ -79,7 +80,7 @@ namespace SpotifyAPI.Web
|
||||
/// <param name="body">The Body-Data (most likely a JSON String)</param>
|
||||
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
|
||||
/// <returns></returns>
|
||||
Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method);
|
||||
Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Uploads data async from an URL and returns the response
|
||||
@ -88,7 +89,7 @@ namespace SpotifyAPI.Web
|
||||
/// <param name="body">The Body-Data (most likely a JSON String)</param>
|
||||
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
|
||||
/// <returns></returns>
|
||||
Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method);
|
||||
Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Uploads data from an URL and converts the response to an object
|
||||
@ -98,7 +99,7 @@ namespace SpotifyAPI.Web
|
||||
/// <param name="body">The Body-Data (most likely a JSON String)</param>
|
||||
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
|
||||
/// <returns></returns>
|
||||
Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method);
|
||||
Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method, Dictionary<string, string> headers = null);
|
||||
|
||||
/// <summary>
|
||||
/// Uploads data async from an URL and converts the response to an object
|
||||
@ -108,25 +109,6 @@ namespace SpotifyAPI.Web
|
||||
/// <param name="body">The Body-Data (most likely a JSON String)</param>
|
||||
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
|
||||
/// <returns></returns>
|
||||
Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method);
|
||||
|
||||
/// <summary>
|
||||
/// Sets a specific Header
|
||||
/// </summary>
|
||||
/// <param name="header">Header name</param>
|
||||
/// <param name="value">Header value</param>
|
||||
void SetHeader(string header, string value);
|
||||
|
||||
/// <summary>
|
||||
/// Removes a specific Header
|
||||
/// </summary>
|
||||
/// <param name="header">Header name</param>
|
||||
void RemoveHeader(string header);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all current Headers
|
||||
/// </summary>
|
||||
/// <returns>A collection of Header KeyValue Pairs</returns>
|
||||
List<KeyValuePair<string, string>> GetHeaders();
|
||||
Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method, Dictionary<string, string> headers = null);
|
||||
}
|
||||
}
|
@ -2115,11 +2115,14 @@ namespace SpotifyAPI.Web
|
||||
Tuple<ResponseInfo, T> response = null;
|
||||
do
|
||||
{
|
||||
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
|
||||
WebClient.SetHeader("Content-Type", "application/json");
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "Authorization", TokenType + " " + AccessToken},
|
||||
{ "Content-Type", "application/json" }
|
||||
};
|
||||
|
||||
if (response != null) { Thread.Sleep(RetryAfter); }
|
||||
response = WebClient.UploadJson<T>(url, uploadData, method);
|
||||
response = WebClient.UploadJson<T>(url, uploadData, method, headers);
|
||||
|
||||
response.Item2.AddResponseInfo(response.Item1);
|
||||
lastError = response.Item2.Error;
|
||||
@ -2142,11 +2145,14 @@ namespace SpotifyAPI.Web
|
||||
Tuple<ResponseInfo, T> response = null;
|
||||
do
|
||||
{
|
||||
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
|
||||
WebClient.SetHeader("Content-Type", "application/json");
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "Authorization", TokenType + " " + AccessToken},
|
||||
{ "Content-Type", "application/json" }
|
||||
};
|
||||
|
||||
if (response != null) { await Task.Delay(RetryAfter).ConfigureAwait(false); }
|
||||
response = await WebClient.UploadJsonAsync<T>(url, uploadData, method).ConfigureAwait(false);
|
||||
response = await WebClient.UploadJsonAsync<T>(url, uploadData, method, headers).ConfigureAwait(false);
|
||||
|
||||
response.Item2.AddResponseInfo(response.Item1);
|
||||
lastError = response.Item2.Error;
|
||||
@ -2204,20 +2210,18 @@ namespace SpotifyAPI.Web
|
||||
|
||||
private Tuple<ResponseInfo, T> DownloadDataAlt<T>(string url)
|
||||
{
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>();
|
||||
if (UseAuth)
|
||||
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
|
||||
else
|
||||
WebClient.RemoveHeader("Authorization");
|
||||
return WebClient.DownloadJson<T>(url);
|
||||
headers.Add("Authorization", TokenType + " " + AccessToken);
|
||||
return WebClient.DownloadJson<T>(url, headers);
|
||||
}
|
||||
|
||||
private Task<Tuple<ResponseInfo, T>> DownloadDataAltAsync<T>(string url)
|
||||
{
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>();
|
||||
if (UseAuth)
|
||||
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
|
||||
else
|
||||
WebClient.RemoveHeader("Authorization");
|
||||
return WebClient.DownloadJsonAsync<T>(url);
|
||||
headers.Add("Authorization", TokenType + " " + AccessToken);
|
||||
return WebClient.DownloadJsonAsync<T>(url, headers);
|
||||
}
|
||||
|
||||
#endregion Util
|
||||
|
@ -4,6 +4,8 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SpotifyAPI.Web.Models;
|
||||
@ -14,198 +16,166 @@ namespace SpotifyAPI.Web
|
||||
{
|
||||
public JsonSerializerSettings JsonSettings { get; set; }
|
||||
|
||||
private readonly WebClient _webClient;
|
||||
private readonly Encoding _encoding = Encoding.UTF8;
|
||||
|
||||
public SpotifyWebClient()
|
||||
public Tuple<ResponseInfo, string> Download(string url, Dictionary<string, string> headers = null)
|
||||
{
|
||||
_webClient = new WebClient()
|
||||
Tuple<ResponseInfo, byte[]> raw = DownloadRaw(url, headers);
|
||||
return new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, string>> DownloadAsync(string url, Dictionary<string, string> headers = null)
|
||||
{
|
||||
Proxy = null,
|
||||
Encoding = _encoding
|
||||
Tuple<ResponseInfo, byte[]> raw = await DownloadRawAsync(url, headers).ConfigureAwait(false);
|
||||
return new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, byte[]> DownloadRaw(string url, Dictionary<string, string> headers = null)
|
||||
{
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> headerPair in headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add(headerPair.Key, headerPair.Value);
|
||||
}
|
||||
}
|
||||
using (HttpResponseMessage response = Task.Run(() => client.GetAsync(url)).Result)
|
||||
{
|
||||
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo()
|
||||
{
|
||||
Headers = ConvertHeaders(response.Headers)
|
||||
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url, Dictionary<string, string> headers = null)
|
||||
{
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> headerPair in headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add(headerPair.Key, headerPair.Value);
|
||||
}
|
||||
}
|
||||
using (HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false))
|
||||
{
|
||||
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo()
|
||||
{
|
||||
Headers = ConvertHeaders(response.Headers)
|
||||
}, await response.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, T> DownloadJson<T>(string url, Dictionary<string, string> headers = null)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response = Download(url, headers);
|
||||
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url, Dictionary<string, string> headers = null)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response = await DownloadAsync(url, headers).ConfigureAwait(false);
|
||||
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, string> Upload(string url, string body, string method, Dictionary<string, string> headers = null)
|
||||
{
|
||||
Tuple<ResponseInfo, byte[]> data = UploadRaw(url, body, method, headers);
|
||||
return new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string body, string method, Dictionary<string, string> headers = null)
|
||||
{
|
||||
Tuple<ResponseInfo, byte[]> data = await UploadRawAsync(url, body, method, headers).ConfigureAwait(false);
|
||||
return new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method, Dictionary<string, string> headers = null)
|
||||
{
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> headerPair in headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add(headerPair.Key, headerPair.Value);
|
||||
}
|
||||
}
|
||||
|
||||
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
|
||||
{
|
||||
Content = new StringContent(body, _encoding)
|
||||
};
|
||||
using (HttpResponseMessage response = Task.Run(() => client.SendAsync(message)).Result)
|
||||
{
|
||||
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
|
||||
{
|
||||
Headers = ConvertHeaders(response.Headers)
|
||||
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method, Dictionary<string, string> headers = null)
|
||||
{
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> headerPair in headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add(headerPair.Key, headerPair.Value);
|
||||
}
|
||||
}
|
||||
|
||||
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
|
||||
{
|
||||
Content = new StringContent(body, _encoding)
|
||||
};
|
||||
using (HttpResponseMessage response = await client.SendAsync(message))
|
||||
{
|
||||
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
|
||||
{
|
||||
Headers = ConvertHeaders(response.Headers)
|
||||
}, await response.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method, Dictionary<string, string> headers = null)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response = Upload(url, body, method, headers);
|
||||
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method, Dictionary<string, string> headers = null)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response = await UploadAsync(url, body, method, headers).ConfigureAwait(false);
|
||||
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_webClient.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, string> Download(string url)
|
||||
private static WebHeaderCollection ConvertHeaders(HttpResponseHeaders headers)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response;
|
||||
try
|
||||
WebHeaderCollection newHeaders = new WebHeaderCollection();
|
||||
foreach (KeyValuePair<string, IEnumerable<string>> headerPair in headers)
|
||||
{
|
||||
Tuple<ResponseInfo, byte[]> raw = DownloadRaw(url);
|
||||
response = new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
|
||||
}
|
||||
catch (WebException e)
|
||||
foreach (string headerValue in headerPair.Value)
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
||||
{
|
||||
response = new Tuple<ResponseInfo, string>(new ResponseInfo
|
||||
{
|
||||
Headers = _webClient.ResponseHeaders
|
||||
}, reader.ReadToEnd());
|
||||
newHeaders.Add(headerPair.Key, headerValue);
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, string>> DownloadAsync(string url)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response;
|
||||
try
|
||||
{
|
||||
Tuple<ResponseInfo, byte[]> raw = await DownloadRawAsync(url).ConfigureAwait(false);
|
||||
response = new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
|
||||
}
|
||||
catch (WebException e)
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
||||
{
|
||||
response = new Tuple<ResponseInfo, string>(new ResponseInfo
|
||||
{
|
||||
Headers = _webClient.ResponseHeaders
|
||||
}, reader.ReadToEnd());
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, byte[]> DownloadRaw(string url)
|
||||
{
|
||||
byte[] data = _webClient.DownloadData(url);
|
||||
ResponseInfo info = new ResponseInfo()
|
||||
{
|
||||
Headers = _webClient.ResponseHeaders
|
||||
};
|
||||
return new Tuple<ResponseInfo, byte[]>(info, data);
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url)
|
||||
{
|
||||
using (WebClient webClient = new WebClient())
|
||||
{
|
||||
webClient.Proxy = null;
|
||||
webClient.Encoding = _encoding;
|
||||
webClient.Headers = _webClient.Headers;
|
||||
|
||||
byte[] data = await _webClient.DownloadDataTaskAsync(url).ConfigureAwait(false);
|
||||
ResponseInfo info = new ResponseInfo()
|
||||
{
|
||||
Headers = webClient.ResponseHeaders
|
||||
};
|
||||
return new Tuple<ResponseInfo, byte[]>(info, data);
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, T> DownloadJson<T>(string url)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response = Download(url);
|
||||
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response = await DownloadAsync(url).ConfigureAwait(false);
|
||||
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, string> Upload(string url, string body, string method)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response;
|
||||
try
|
||||
{
|
||||
Tuple<ResponseInfo, byte[]> data = UploadRaw(url, body, method);
|
||||
response = new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
|
||||
}
|
||||
catch (WebException e)
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
||||
{
|
||||
response = new Tuple<ResponseInfo, string>(new ResponseInfo
|
||||
{
|
||||
Headers = _webClient.ResponseHeaders
|
||||
}, reader.ReadToEnd());
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string body, string method)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response;
|
||||
try
|
||||
{
|
||||
Tuple<ResponseInfo, byte[]> data = await UploadRawAsync(url, body, method).ConfigureAwait(false);
|
||||
response = new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
|
||||
}
|
||||
catch (WebException e)
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
||||
{
|
||||
response = new Tuple<ResponseInfo, string>(new ResponseInfo
|
||||
{
|
||||
Headers = _webClient.ResponseHeaders
|
||||
}, reader.ReadToEnd());
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method)
|
||||
{
|
||||
byte[] data = _webClient.UploadData(url, method, _encoding.GetBytes(body));
|
||||
ResponseInfo info = new ResponseInfo
|
||||
{
|
||||
Headers = _webClient.ResponseHeaders
|
||||
};
|
||||
return new Tuple<ResponseInfo, byte[]>(info, data);
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method)
|
||||
{
|
||||
using (WebClient webClient = new WebClient())
|
||||
{
|
||||
webClient.Proxy = null;
|
||||
webClient.Encoding = _encoding;
|
||||
webClient.Headers = _webClient.Headers;
|
||||
byte[] data = await _webClient.UploadDataTaskAsync(url, method, _encoding.GetBytes(body)).ConfigureAwait(false);
|
||||
ResponseInfo info = new ResponseInfo
|
||||
{
|
||||
Headers = _webClient.ResponseHeaders
|
||||
};
|
||||
return new Tuple<ResponseInfo, byte[]>(info, data);
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response = Upload(url, body, method);
|
||||
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
||||
}
|
||||
|
||||
public async Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method)
|
||||
{
|
||||
Tuple<ResponseInfo, string> response = await UploadAsync(url, body, method).ConfigureAwait(false);
|
||||
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
||||
}
|
||||
|
||||
public void SetHeader(string header, string value)
|
||||
{
|
||||
_webClient.Headers[header] = value;
|
||||
}
|
||||
|
||||
public void RemoveHeader(string header)
|
||||
{
|
||||
if (_webClient.Headers[header] != null)
|
||||
_webClient.Headers.Remove(header);
|
||||
}
|
||||
|
||||
public List<KeyValuePair<string, string>> GetHeaders()
|
||||
{
|
||||
return _webClient.Headers.AllKeys.Select(header => new KeyValuePair<string, string>(header, _webClient.Headers[header])).ToList();
|
||||
return newHeaders;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user