[英]Error 405, method not allowed on HTTPRequest in C#
我正在尝试使用megaupload api通过C#获取我的文件的信息。 甚至我的请求也应该非常快,我正在asynchronously
执行它。 根据他们在其网站上的指示,关于curl
的请求应如下所示: curl https://megaupload.nz/api/v2/file/u1C0ebc4b0/info
我不知道是否是我在理解整个问题,但是我收到一条error
指出“ EnsureSuccessStatusCode
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;
}
}
看这行:
using (HttpResponseMessage response = await client.PostAsync(url, postContent))
PostAsync
将POST
请求发送到服务器,而您应该发送GET
请求。 更换PostAsync
与GetAsync
,摆脱多余的postParams
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.