伙计们,我有以下代码:
public static CookieContainer cookies;
public static HttpWebRequest GetNewRequest(string targetUrl, CookieContainer SessionCookieContainer)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl);
request.CookieContainer = SessionCookieContainer;
request.AllowAutoRedirect = false;
return request;
}
public async static Task<HttpWebResponse> MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary<string, string> parameters = null)
{
HttpWebResponse response;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Accept: */*";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.CookieContainer = SessionCookieContainer;
request.AllowAutoRedirect = false;
if (parameters != null)
{
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string s = "";
foreach (KeyValuePair<string, string> pair in parameters)
{
if (s.Length == 0)
{
s = s + string.Format("{0}={1}", pair.Key, pair.Value);
}
else
{
s = s + string.Format("&{0}={1}", pair.Key, pair.Value);
}
}
byte[] bytes = Encoding.UTF8.GetBytes(s);
using (Stream stream = await request.GetRequestStreamAsync())
{
stream.Write(bytes, 0, bytes.Length);
}
}
request.Method = "GET";
response = await request.GetResponseAsync() as HttpWebResponse;
SessionCookieContainer.Add(response.Cookies);
while (response.StatusCode == HttpStatusCode.Found)
{
response.Close();
request = GetNewRequest(response.Headers["Location"], SessionCookieContainer);
response = await request.GetResponseAsync() as HttpWebResponse;
SessionCookieContainer.Add(response.Cookies);
}
return response;
}
我使用此功能某些方法(例如)
async Task<string> login(string url, string id, string pw)
{
///code...
}
我的问题是:如果我想在buttonclick(object sender, EventArgs e)
得到结果,我该怎么办?
我已经尝试过这个,但不起作用:
private void buttonclick(object sender, EventArgs e)
{
string htmlPage=login(url, id, pw);
}
编辑
我已经解决了在 private
和 void 之间添加async
并在login(bla bla)
之前添加await
的问题
虽然以下是一个明显的解决方案,但它有隐藏的陷阱:
private async void buttonclick(object sender, EventArgs e)
{
string htmlPage = await login(url, id, pw);
}
- 如果
login
引发异常怎么办?它不会被观察到,很可能会使应用程序崩溃。 - 如果用户单击按钮两次,而上一个异步登录操作已在进行中,该怎么办?您当然不想要两个挂起的登录。
这是一个略微改进的版本:
Task<string> _pendingLogin = null;
private async void buttonclick(object sender, EventArgs e)
{
if (_pendingLogin != null)
{
MessageBox.Show("Login pending...");
return;
}
try
{
_pendingLogin = login(url, id, pw);
string htmlPage = await _pendingLogin;
MessageBox.Show("Logged in: " + htmlPage);
}
catch(Exception ex)
{
MessageBox.Show("Error in login: " + ex.Message);
}
_pendingLogin = null;
}
您的登录方法是异步。简单来说,异步方法在调用时不会返回结果值,相反,异步方法返回 TResult 的任务(登录方法的字符串任务)。任务是一种特殊类型,它表示您计算结果值的承诺。为了获得结果,您应该:
- 叫。Tast 对象上的结果属性。这将阻止调用方线程,直到计算结果。(对于Windows应用程序非常糟糕,因此它使应用程序无响应,甚至导致死锁(取决于同步方法实现)
- 使用 await 关键字异步等待,直到计算结果值。这将使调用方 (UI) 线程在下载网页时自由执行其他内容。一旦任务完成(下载网页),UI 线程将在 await 语句之后立即返回到处理代码。这是 Windows 应用的首选,因为这允许应用保持响应。
试试这个:
private async void buttonclick(object sender, EventArgs e)
{
string htmlPage = await login(url, id, pw);
}