我们有以下代码:
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);
}
修改
我已解决了在async
和空格之间添加private
并在await
login(bla bla)
的问题
答案 0 :(得分:2)
您的登录方式是Async。简单来说,异步方法在调用时不会返回结果值,而是异步方法返回TResult的任务(登录方法的字符串任务)。 Task是一种特殊类型,它代表您计算结果值的承诺。为了得到结果,您应该:
试试这个:
private async void buttonclick(object sender, EventArgs e)
{
string htmlPage = await login(url, id, pw);
}
答案 1 :(得分:2)
虽然以下是一个明显的解决方案,但它隐藏了捕获:
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;
}