Ошибка 405, метод не разрешен для HTTPRequest в C # - PullRequest
0 голосов
/ 02 октября 2018

Я пытаюсь использовать megaupload api , чтобы получить информацию о моих файлах с помощью C #.Даже если запрос должен быть очень быстрым, я делаю это asynchronously.Согласно их инструкциям на их веб-сайте, запрос на curl должен выглядеть следующим образом: curl https://megaupload.nz/api/v2/file/u1C0ebc4b0/info Я не знаю, неправильно ли я понимаю все это здесь, но я получаю error высказывание Method Not Allowed от EnsureSuccessStatusCode

Вот мой код:

static void Main(string[] args)
        {
            //var urlApiFormat = https://megaupload.nz/api/v2/file/{id}/info
            //var myURL = https://megaupload.nz/L8ydu8i3b6/Mystic_v1.1_rar
            Task.Run(() =>
            {
                var sReturn = megauploadSharpAsync(@"https://megaupload.nz/api/v2/file/L8ydu8i3b6/info");
                Console.WriteLine("Result: " + sReturn.Result);
            });

            Console.ReadKey();
        }

public static async Task<string> megauploadSharpAsync(string url)//, string outputFile)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var postParams = new Dictionary<string, string>();

                    postParams.Add("method", "GET");
                    //postParams.Add("api_key", "keyforaccountupload");

                    using (var postContent = new FormUrlEncodedContent(postParams))
                    using (HttpResponseMessage response = await client.PostAsync(url, postContent))
                    {
                        response.EnsureSuccessStatusCode(); // Throw if httpcode is an error
                        using (HttpContent content = response.Content)
                        {
                            string result = await content.ReadAsStringAsync();
                            return result;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                await Task.Run(() =>
                {
                    //some magic
                    Console.WriteLine("Error: " + ex.Message);
                });
                return string.Empty;
            }
        }

1 Ответ

0 голосов
/ 02 октября 2018

Посмотрите на эту строку:

using (HttpResponseMessage response = await client.PostAsync(url, postContent))

PostAsync отправляет POST запрос на сервер, в то время как вы должны вместо этого отправить GET запрос.Замените PostAsync на GetAsync и избавьтесь от лишних postParams

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...