我有一个简单的请求函数,可以让用户登录。我刚刚了解了 aysnc 方法(我是 C# 初学者),并看到可以使用 async 和 await 发出请求。但是我很难弄清楚如何转换我现有的代码。我在 stackoverflow 上阅读了类似的问题,但仍然无法使其与我的代码一起使用。
public static string LogIn(string email, string password)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost");
request.Method = "POST"
request.ContentType = "application/json";
request.CookieContainer = cookies;
CredentialClass credentials = new CredentialClass();
credentials.email = email
credentials.password = password;
var ser = new DataContractJsonSerializer(typeof(CredentialClass));
ser.WriteObject(request.GetRequestStream(), credentials);
var response = (HttpWebResponse)request.GetResponse();
return (response.StatusCode.ToString());
}
关于如何使它工作的任何建议?