使用 await httpClient 的异步调用不起作用
async call with await httpClient not working
我想获取网站的一些信息,但是在使用 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) 然后中断并转到下一行是 return vie (bigModel) 在 ActionResult OutputTable
您应该使您的操作异步并等待 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);
}
我想获取网站的一些信息,但是在使用 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) 然后中断并转到下一行是 return vie (bigModel) 在 ActionResult OutputTable
您应该使您的操作异步并等待 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);
}