所以我一直在摆弄C#中的Web响应和请求,当我尝试运行程序时,我遇到了一个问题。我有一行代码:
var response = await httpClient.SendAsync(request);
在方法制作中需要async
private static void Main(string[] args)
成
private static async Task Main(string[] args)
看起来没有错误,但在构建时,我收到了错误消息:
程序不包含适用于入口点的静态“Main”方法。
这是我的代码
private static async Task Main(string[] args)
{
try
{
/*HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://billing.roblox.com/v1/gamecard/redeem");
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
JsonExtensionDataAttribute data = new JsonExtensionDataAttribute();
data = '3335996838'*/
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://billing.roblox.com/v1/gamecard/redeem"))
{
request.Headers.TryAddWithoutValidation("Accept", "application/json");
request.Content = new StringContent("3335996838", Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request);
Console.WriteLine(request.Content);
Console.ReadLine();
}
}
}
catch (WebException ex)
{
string content;
using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
{
content = reader.ReadToEnd();
Console.WriteLine(content);
Console.ReadLine();
}
}
}
有人请帮帮我!
我怀疑你还在使用Visual Studio 2017,因为async Main
works out of the box for new projects on Visual Studio 2019。
要允许async Task Main
,您需要指定LatestMinor
作为您的语言版本。 (构建 - >高级 - >语言版本)。 async Main
是一种C#7.1语言功能,在VS2017下,新项目默认为C#7.0。
它是main方法的必需void返回类型。您可以返回void main并更改此行:
var response = await httpClient.SendAsync(request);
至
var response = httpClient.SendAsync(request).Result;
.Result获取任务的结果值。