Answer the question
In order to leave comments, you need to log in
Why is the code throwing http 451 error?
The code sends a Post request to this site, but returns a 451 error . What's
the catch???
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using CefSharp;
using CefSharp.WinForms;
using System.Text;
using System.Text.Json;
namespace WinFormsApp4
{
public partial class Form1 : Form
{
private ChromiumWebBrowser browser;
private readonly HttpClient http = new HttpClient();
string Wasm;
string Address = "https://kad.arbitr.ru/Kad/SearchInstances";
class Search {
public int Page { get; set; }
public int Count { get; set; }
public string[] Courts { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public Sides[] Sides { get; set; }
public Judges[] Judges { get; set; }
public string[] CaseNumbers { get; set; }
public bool WithVKSInstances { get; set; }
}
class Sides
{
public string Name { get; set; }
public int Type { get; set; }
public bool ExactMatch { get; set; }
}
class Judges
{
public string JudgeId { get; set; }
public int Type { get; set; }
}
public Form1()
{
InitializeComponent();
InitializeChromium();
}
private async void button1_Click(object sender, EventArgs e)
{
Search search = new Search()
{
CaseNumbers = Array.Empty<string>(),
Page = 1,
Count = 25,
Courts = Array.Empty<string>(),
DateFrom = null,
DateTo = null,
Sides = Array.Empty<Sides>(),
Judges = Array.Empty<Judges>(),
WithVKSInstances = false
};
try
{
var response = await PostJsonAsync(Address, search);
MessageBox.Show(response);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private async Task<string> PostJsonAsync(string url, Search obj)
{
UpdateWasm();
HttpContent content = new StringContent(JsonSerializer.Serialize(obj));
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
var cookies = await Cef.GetGlobalCookieManager().VisitAllCookiesAsync();
var wasm = cookies.First(x => x.Name == "wasm").Value;
request.Content = content;
request.Headers.Add("Cookie", $"wasm={wasm}");
HttpResponseMessage response = await http.SendAsync(request, HttpCompletionOption.ResponseContentRead);
return await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
}
private void InitializeChromium()
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
browser = new ChromiumWebBrowser("https://kad.arbitr.ru/");
Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
private void button2_Click(object sender, EventArgs e)
{
UpdateWasm();
}
private void UpdateWasm()
{
string script = "const btn = document.getElementById('b-form-submit').children[0].children[1]; btn.click();";
browser.ExecuteScriptAsync(script);
}
}
}
Answer the question
In order to leave comments, you need to log in
https://ru.wikipedia.org/wiki/HTTP_451
Error 451 or “Unavailable For Legal Reasons” is a standard HTTP response code that means that access to the resource is closed, for example, at the request of government authorities or the copyright holder in case of copyright infringement. It was approved by the IESG on December 21, 2015[1] and published as RFC 7725 in February 2016. The error code is a reference to Ray Bradbury's novel Fahrenheit 451. We can say that the HTTP 451 code is a clarifying version of the HTTP 403 code[3].
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question