Search code examples
c#asp.net-mvcscrapysharp

How do I get scrapysharp to work in a MVC web app?


I successfully have scrapysharp working in a console app.

I created a new MVC web app in VS2013 with no authentication or anything else special. I used nuget to add ScrapySharp and then have this code in my Home Controller. I get no response to my pageresult. Does anyone have any ideas why? How do I get scrapysharp to work in a MVC web app?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrapySharp.Html.Forms;
using ScrapySharp.Network;
using ScrapySharp;
using HtmlAgilityPack;
using ScrapySharp.Extensions;

namespace scrapyB.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
           var  browser = new ScrapingBrowser();
            browser.AllowAutoRedirect = true;
            browser.AllowMetaRedirect = true;
           var pageresult = browser.NavigateToPage(new Uri("https://google.com"));
           var x = pageresult;
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

Edit Actually I just tried a windows Forms application and NavigateToPage doesn't return a result either. It's weird but it seems as it only works in a console app. Does anyone know how to get ScrapySharp to work in something other than a console app?


Solution

  • Ok so scrapysharp has a NavigateToPageAsync function. This returns a result as expected.

    Form.submit() uses NavigateToPage as well but you can instead use NavigateToPageAsync and pass in the correct parameters to submit the form.