【问题标题】:async call with await httpClient not working带有等待 httpClient 的异步调用不起作用
【发布时间】:2019-10-10 21:45:18
【问题描述】:

我想获取一些网站信息,但是在使用 await 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)

【问题讨论】:

  • 在 bigModel.Crawler 处添加 await,否则线程将不会等待另一步完成。
  • 你没有在等电话。
  • 但 await 仅适用于异步方法
  • @KhanhDuy 那是 错误。你也应该等待Crawler()。这也是一种异步方法。无需等待,您的操作将在Crawler() 有机会完成之前完成并返回

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


【解决方案1】:

您应该使您的操作异步并等待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);
}

【讨论】:

    猜你喜欢
    • 2018-06-06
    • 2021-06-14
    • 2023-04-07
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多