我正在使用这种方法来获取谷歌建议查询:
public void GetSuggestFromGoogle(string query)
{
try
{
query = HttpUtility.UrlEncode(query);
string targetUrl = "http://suggestqueries.google.com/complete/search?ds=yt&output=toolbar&hl=en-US&q=" + query;
UTF8Encoding utf8 = new UTF8Encoding();
byte[] bytes = utf8.GetBytes(targetUrl);
targetUrl = targetUrl.ToString();
request = HttpWebRequest.Create(new Uri(targetUrl)) as HttpWebRequest;
request.Method = "GET";
IsDownload = true;
request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);
}
catch
{
IsDownload = false;
failCallback();
}
}
在我调用这个方法之前,我使用:
public void CancelGoogleRequest()
{
if (IsDownload)
{
request.Abort();
IsDownload = false;
}
}
这是完成请求方法:
private void FinishWebRequest(IAsyncResult result)
{
try
{
HttpWebResponse wResponse = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
Stream stream = wResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string xml = reader.ReadToEnd();
List<string> list = this.ParseXml(xml);
finishCallback(list);
IsDownload = false;
}
catch//(Exception erere)
{
IsDownload = false;
failCallback();
}
}
我的问题是 Abort HttpWebRequest
,即使我在调用新FinishWebRequest
方法之前中止请求,也可以取消它?