Rucaptcha.com asks you to send a base64 form in this format:

<form method="post" action="http://rucaptcha.com/in.php"> <input type="hidden" name="method" value="base64"> Ваш ключ: <input type="text" name="key" value="YOUR_APIKEY"> Тело файла капчи в формате base64: <textarea name="body">BASE64_FILE</textarea> <input type="submit" value="Загрузить и получить ID"> </form> 

I have my ApiKey and a picture in BASE64. How do I send this to the server?

    1 answer 1

    In general, is it not easier to use the library for this, for example this one ? Although ... it is in this case superfluous.

    Well, and so, the code is about the following:

     public async Task<string> SendRequest(CancellationToken ct) { string data; var baseAddress = new Uri("http://rucaptcha.com"); //Базовый адрес var url = "/in.php"; //Нужная нам страница, на которую пойдет запрос using (var client = new HttpClient { BaseAddress = baseAddress }) { var content = new FormUrlEncodedContent(new[] //для удобства можно использовать Dictionary<string, string>. Тогда тело будет ещё короче ["key"] = "YOUR_APIKEY", ["body"] = "BASE64_FILE" { new KeyValuePair<string, string>("key", "YOUR_APIKEY"), new KeyValuePair<string, string>("body", "BASE64_FILE") }); //Наше тело, которое при помощи FormUrlEncodedContent закодируется в нужное нам "тело". var result = await client.PostAsync(url, content, ct); //Отправляем на нужную страницу POST запрос с нашем телом, также тут используется CancellationToken для грамотной отмены async методов. var bytes = await result.Content.ReadAsByteArrayAsync(); Encoding encoding = Encoding.GetEncoding("utf-8"); data = encoding.GetString(bytes, 0, bytes.Length); //Все эти три строки добавлены тут для того, что бы получать данные в нужной нам кодировке (некоторые сервера к примеру выдают в неверной кодировке и может выдать ошибку). Вообще можно все 3 строки заменить на одну: //data = await result.Content.ReadAsStringAsync(); Тогда кодировка будет той, что выдает сервер. result.EnsureSuccessStatusCode(); } return data; } 

    I think the comments will understand. It's all pretty simple here.

    • Thank you so much :) - Twilight Owl
    • @TwilightOwl If the answer is right for you, then check it with a checkmark on the side. Well, so on health. - EvgeniyZ
    • one
      Instead of KeyValuePair<string, string>[] you can use a Dictionary<string, string> - the syntax will be a bit more compact: new Dictionary<string, string> { ["key"] = "YOUR_APIKEY", ["body"] = "BASE64_FILE" } - Andrey NOP
    • 2
      Again bicycles, there is a ready-made library C # bohdash.com/works/Rucaptcha-CSharp with its help, the code will take one line. - Digital Core
    • @DigitalCore Here you are wrong, this is not a bicycle, but something that will be part of your library. If a person just needs to send a request, then why does he need an extra library with a bunch of extra classes? After all, this is the simplest request for the simplest API . For example, I am a supporter of portable applications and as few third-party libraries as possible, and if in one file, it is generally gorgeous!) Then you will depend on them. Yes, if the library is developed, there is a lot of community, then ok, but others .. What if they abandon it? What's next? All code from scratch when abandoning it? - EvgeniyZ