与await httpClient的异步调用不起作用

时间:2019-05-24 09:49:56

标签: .net asp.net-mvc async-await task-parallel-library

我想获取一些网站信息,但是在使用httpClient.GetStringAsync时在我的代码中出现了一些问题

我使用Visual Studio 2017在ASP MVC上进行编码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web_Crawler.Models;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Net.Http;
namespace Web_Crawler.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult OutputTable(string Name, string ClassChildName)
        {
            var bigModel = new BigModel();
            bigModel.url.Name = Name;
            bigModel.url.ClassChildName = ClassChildName;
            bigModel.Crawler();
            return View(bigModel);
        }
     }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web_Crawler.Models;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Net.Http;
namespace Web_Crawler.Models
{
    public class BigModel
    {
        public ListProduct products;
        public URL url;
        public BigModel()
        {
            products = new ListProduct();
            url = new URL();
        }
        public async Task Crawler()
        {
            var Url = url.Name;
            var httpClient = new HttpClient();
            var html = await httpClient.GetStringAsync(Url);
            var htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(html);

            var divs = htmlDocument.DocumentNode.Descendants("div")
                .Where(node => node.GetAttributeValue("class", "").Equals(url.ClassChildName)).ToList();
            foreach (var div in divs)
            {
                var div1 = htmlDocument.DocumentNode.Descendants("div")
                    .Where(x => x.GetAttributeValue("class", "").Equals("product-row-info")).ToList();
                var product = new Product();
                foreach (var div2 in div1)
                {
                    product.Price = div2.Descendants("span").FirstOrDefault().InnerText;
                }
                product.Model = div.Descendants("h2").FirstOrDefault().InnerText;
                product.Link = div.Descendants("a").FirstOrDefault().ChildAttributes("href").FirstOrDefault().Value;
                product.ImageUrl = div.Descendants("img").FirstOrDefault().ChildAttributes("src").FirstOrDefault().Value;
                products.Products.Add(product);
            }
        }
    }
}

当我调试时,代码运行到var html = await httpClient.GetStringAsyns(Url),然后中断并转到下一行,在ActionResult OutputTable中返回vie(bigModel)

1 个答案:

答案 0 :(得分:3)

您应该使操作异步,并等待Crawler方法的调用:

public async Task<ActionResult> OutputTable(string Name, string ClassChildName)
{
   var bigModel = new BigModel();
   bigModel.url.Name = Name;
   bigModel.url.ClassChildName = ClassChildName;
   await bigModel.Crawler();

   return View(bigModel);
}