diff --git a/VkNet.Tests/Categories/Users/UsersCategoryTest.cs b/VkNet.Tests/Categories/Users/UsersCategoryTest.cs index 9b193f659..516e10d75 100644 --- a/VkNet.Tests/Categories/Users/UsersCategoryTest.cs +++ b/VkNet.Tests/Categories/Users/UsersCategoryTest.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -560,7 +560,7 @@ public void Get_NotAccessToInternet_ThrowVkApiException() { Mock.Get(Api.RestClient) .Setup(f => - f.PostAsync(It.IsAny(), It.IsAny>>(), Encoding.UTF8)) + f.PostAsync(It.IsAny(), It.IsAny>>(), Encoding.UTF8, null)) .Throws(new VkApiException("The remote name could not be resolved: 'api.vk.com'")); FluentActions.Invoking(() => Api.Users.Get(new long[] diff --git a/VkNet.Tests/Infrastructure/BaseTest.cs b/VkNet.Tests/Infrastructure/BaseTest.cs index b023b196f..121f19c6e 100644 --- a/VkNet.Tests/Infrastructure/BaseTest.cs +++ b/VkNet.Tests/Infrastructure/BaseTest.cs @@ -89,7 +89,7 @@ public BaseTest() Mocker.Setup>>(x => x.PostAsync(It.Is(s => s == new Uri(Url)), It.IsAny>>(), - It.IsAny())) + It.IsAny(), null)) .Callback(Callback) .Returns(() => { @@ -103,7 +103,7 @@ public BaseTest() Mocker.Setup>>(x => x.PostAsync(It.Is(s => string.IsNullOrWhiteSpace(Url)), It.IsAny>>(), - It.IsAny())) + It.IsAny(), null)) .Throws(); Api = Mocker.CreateInstance(); diff --git a/VkNet/Abstractions/Utils/IRestClient.cs b/VkNet/Abstractions/Utils/IRestClient.cs index 272a2d853..2f6934d8b 100644 --- a/VkNet/Abstractions/Utils/IRestClient.cs +++ b/VkNet/Abstractions/Utils/IRestClient.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Net; using System.Text; @@ -40,6 +40,7 @@ public interface IRestClient : IDisposable /// Uri /// Параметры /// + /// Заголовки /// Строковый результат - Task> PostAsync(Uri uri, IEnumerable> parameters, Encoding encoding); + Task> PostAsync(Uri uri, IEnumerable> parameters, Encoding encoding, IEnumerable> headers = null); } \ No newline at end of file diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/Forms/AbstractAuthorizationForm.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/Forms/AbstractAuthorizationForm.cs index 5d7846971..d85359774 100644 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/Forms/AbstractAuthorizationForm.cs +++ b/VkNet/Infrastructure/Authorization/ImplicitFlow/Forms/AbstractAuthorizationForm.cs @@ -32,7 +32,7 @@ public async Task ExecuteAsync(Uri url, IApiAuthParams FillFormFields(form, authParams); - var response = await _restClient.PostAsync(new(form.Action), form.Fields, Encoding.GetEncoding(1251)) + var response = await _restClient.PostAsync(new(form.Action), form.Fields, Encoding.UTF8, form.Headers) .ConfigureAwait(false); if (!response.IsSuccess) diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/Forms/ImplicitFlowLoginForm.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/Forms/ImplicitFlowLoginForm.cs index 0d37a9900..b90d5323d 100644 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/Forms/ImplicitFlowLoginForm.cs +++ b/VkNet/Infrastructure/Authorization/ImplicitFlow/Forms/ImplicitFlowLoginForm.cs @@ -29,5 +29,12 @@ protected override void FillFormFields(VkHtmlFormResult form, IApiAuthParams aut { form.Fields[AuthorizationFormFields.Password] = authParams.Password; } + + form.Headers = new System.Collections.Generic.Dictionary + { + { "content-type", "application/x-www-form-urlencoded" }, + { "origin", "https://oauth.vk.com" }, + { "referer", "https://oauth.vk.com/" } + }; } } \ No newline at end of file diff --git a/VkNet/Infrastructure/Authorization/ImplicitFlow/VkHtmlFormResult.cs b/VkNet/Infrastructure/Authorization/ImplicitFlow/VkHtmlFormResult.cs index cf60bddeb..40c1a9beb 100644 --- a/VkNet/Infrastructure/Authorization/ImplicitFlow/VkHtmlFormResult.cs +++ b/VkNet/Infrastructure/Authorization/ImplicitFlow/VkHtmlFormResult.cs @@ -26,4 +26,9 @@ public class VkHtmlFormResult /// Поля формы /// public Dictionary Fields { get; set; } + + /// + /// Заголовки для запросов + /// + public Dictionary Headers { get; set; } } \ No newline at end of file diff --git a/VkNet/Utils/RestClient.cs b/VkNet/Utils/RestClient.cs index 79ca9828f..741259114 100644 --- a/VkNet/Utils/RestClient.cs +++ b/VkNet/Utils/RestClient.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using JetBrains.Annotations; @@ -50,7 +51,7 @@ public Task> GetAsync(Uri uri, IEnumerable - public Task> PostAsync(Uri uri, IEnumerable> parameters, Encoding encoding) + public Task> PostAsync(Uri uri, IEnumerable> parameters, Encoding encoding, IEnumerable> headers = null) { if (_logger != null) { @@ -58,6 +59,19 @@ public Task> PostAsync(Uri uri, IEnumerable { + if (header.Key.ToLower() == "content-type") + { + HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Value)); + } else + { + HttpClient.DefaultRequestHeaders.Add(header.Key, header.Value); + } + }); + } + var content = new FormUrlEncodedContent(parameters); return CallAsync(() => HttpClient.PostAsync(uri, content), encoding);