可以从Html Agility Pack HttpWebResponse
获取HtmlWeb
吗?
如同,如果我打电话:
HtmlWeb web = new HtmlWeb ();
HtmlDocument document = web.Load ("http://stackoverflow.com/");
是否有方便的方法从HttpWebResponse
实例中检索web
以查看标题 - 例如检索内容类型等标题属性?
有前任和后任代表,可以工作:
HtmlWeb web = new HtmlWeb ();
web.PreRequest = delegate (HttpWebRequest request) {
request.Timeout = 10 * 1000;
return true;
};
web.PostResponse = delegate (HttpWebRequest request, HttpWebResponse response) {
//response.Headers;
//response.ContentType;
};
HtmlDocument document = web.Load ("http://stackoverflow.com/");
相反,有没有办法让响应像:(HttpWebResponse)web.GetResponse();
答案 0 :(得分:3)
您可以使用TaskCompletionSource
+ async / await
var web = new HtmlAgilityPack.HtmlWeb();
var tcs = new TaskCompletionSource<HttpWebResponse>();
web.PostResponse = delegate(HttpWebRequest request, HttpWebResponse response)
{
tcs.SetResult(response);
};
var document = web.Load("http://stackoverflow.com/");
var httpWebResponse = await tcs.Task;