我是 C# 新手,我很可能误解了 await、async 和 Tasks 的正确用法:)
我想开发一个类 ( OWConnector
) 作为我的应用程序的 API 客户端,为此我开发了一个通用PostRequest
方法来执行 POST 请求。
PostRequest
不幸的是,当我使用 auth 方法(使用通用方法)时,该应用程序看起来陷入了死锁。
你能帮我理解问题出在哪里吗?我在代码中标记了调试器永远等待的地方。
// the debugger get stacked here :(
表格方法
private void btnAnalyze_Click(object sender, EventArgs e)
{
OWConnector api = new OWConnector(@"http://mywebsite.com/");
Boolean didAuth = api.auth("myuser", "mypass");
if (didAuth)
{
MessageBox.Show(@"success :)");
}
else
{
MessageBox.Show(@"failed :(");
}
}
OWConnector 类
class OWConnector
{
private CookieContainer cookieJar;
private HttpClientHandler handler;
private HttpClient client;
private Uri baseUri;
public OWConnector(string baseUrl)
{
baseUri = new Uri(baseUrl);
cookieJar = new CookieContainer();
handler = new HttpClientHandler();
handler.CookieContainer = cookieJar;
handler.UseCookies = true;
handler.AllowAutoRedirect = false;
client = new HttpClient(handler);
client.BaseAddress = baseUri;
}
public async Task<RequestResponse> PostRequest(string url, HttpContent data = null)
{
RequestResponse response = new RequestResponse();
try
{
// the debugger get stacked here :(
response.httpResponse = await client.PostAsync(url, data);
}
catch (System.AggregateException e)
{
response.error = true;
foreach (Exception ie in e.InnerExceptions)
{
response.errorMessage += ie.GetType().ToString() + ": " + ie.Message + "\n";
}
}
return response;
}
public Boolean auth(string username, string password)
{
var content = new FormUrlEncodedContent(new[]{
new KeyValuePair<string, string>(@"form_name", @"sign-in"),
new KeyValuePair<string, string>(@"identity", username),
new KeyValuePair<string, string>(@"password", password),
new KeyValuePair<string, string>(@"remember", @"on"),
new KeyValuePair<string, string>(@"submit", @"Sign In"),
});
RequestResponse r = PostRequest(@"/", content).Result;
Boolean cookieFound = false;
foreach (Cookie c in cookieJar.GetCookies(baseUri))
{
if (c.Name == @"ow_login")
{
cookieFound = true;
}
}
return cookieFound;
}
}
class RequestResponse
{
public Boolean error;
public string errorMessage;
public HttpResponseMessage httpResponse;
public RequestResponse()
{
error = false;
errorMessage = @"";
}
}