C# 错误405,在C中的HTTPRequest上不允许使用方法#
我正在尝试使用C#获取我的文件信息。 即使请求速度应该非常快,我也在异步执行C# 错误405,在C中的HTTPRequest上不允许使用方法#,c#,http,curl,C#,Http,Curl,我正在尝试使用C#获取我的文件信息。 即使请求速度应该非常快,我也在异步执行。 根据他们网站上的说明,curl上的请求应该是这样的:curlhttps://megaupload.nz/api/v2/file/u1C0ebc4b0/info 我不知道我是否理解错了这里的全部内容,但我收到了一个错误说方法不允许来自ensureccessstatuscode 这是我的密码: static void Main(string[] args) { //var urlA
。
根据他们网站上的说明,curl
上的请求应该是这样的:curlhttps://megaupload.nz/api/v2/file/u1C0ebc4b0/info
我不知道我是否理解错了这里的全部内容,但我收到了一个错误
说方法不允许
来自ensureccessstatuscode
这是我的密码:
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;
}
}
static void Main(字符串[]args)
{
//变量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(“结果:+sReturn.Result”);
});
Console.ReadKey();
}
公共静态异步任务megauploadSharpAsync(字符串url)/,字符串输出文件)
{
尝试
{
使用(HttpClient=new HttpClient())
{
var postParams=新字典();
postParams.Add(“方法”、“获取”);
//postParams.Add(“api_key”、“keyforaccountupload”);
使用(var postContent=newformurlencodedcontent(postParams))
使用(httpresponsemessageresponse=wait client.PostAsync(url,postContent))
{
response.EnsureSuccessStatusCode();//如果httpcode出错,则抛出
使用(HttpContent=response.content)
{
字符串结果=等待内容。ReadAsStringAsync();
返回结果;
}
}
}
}
捕获(例外情况除外)
{
等待任务。运行(()=>
{
//一些魔法
Console.WriteLine(“错误:+ex.Message”);
});
返回字符串。空;
}
}
看这一行:
using (HttpResponseMessage response = await client.PostAsync(url, postContent))
PostAsync
向服务器发送POST
请求,而您应该发送GET
请求。将PostAsync
替换为GetAsync
并去除冗余的postaparam