所以我一直在摆弄C#中的Web响应和请求,当我尝试运行该程序时遇到了问题。我有的一行代码:
var response = await httpClient.SendAsync(request);
方法制作中所需的异步
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
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;
.结果是获取任务的结果值。