John P.
John P.

Reputation: 1249

Error 405, method not allowed on HTTPRequest in C#

I am trying to use megaupload api to get information of my files with C#. Even tho request should be very fast i am doing it asynchronously. According to their instructions on their website a request on curl should look like this: curl https://megaupload.nz/api/v2/file/u1C0ebc4b0/info I don't know if it is me understanding wrong the entire thing here but i am getting an error saying Method Not Allowed from EnsureSuccessStatusCode

Here is my code:

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;
            }
        }

Upvotes: 0

Views: 941

Answers (1)

ingvar
ingvar

Reputation: 4375

Look at this line:

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

PostAsync sends POST request to server, while you should send GET request instead. Replace PostAsync with GetAsync and get rid of redundant postParams

Upvotes: 1

Related Questions