简体   繁体   English

从C#桌面应用程序调用ASP.NET脚本

[英]Call a ASP.NET script from a C# desktop app

I'm trying to develop a desktop app to be used as a website scraping tool. 我正在尝试开发一个桌面应用程序以用作网站抓取工具。 My requirement is the user should be able to specify a url in the desktop app.The desktop app should be able to invoke the asp.net script to scrape data from the website and return the records to the desktop app. 我的要求是用户应该能够在桌面应用程序中指定一个URL。桌面应用程序应该能够调用asp.net脚本从网站抓取数据并将记录返回到桌面应用程序。

Should I use a web service or a ASP.NET runtime for this...??? 我应该为此使用Web服务还是ASP.NET运行时... ???

Any help is appreciated :) 任何帮助表示赞赏:)

Additional details 额外细节

The scraping activity is already done.I used HTMLAgility pkg. 抓取活动已经完成。我使用了HTMLAgility pkg。 This is my scraping code to extract a list of company names from a web page. 这是我的抓取代码,用于从网页中提取公司名称列表。

public static String getPageHTML(String URL)
        {
            String totalCompanies = null;
            HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);

            IWebProxy myProxy = httpWebRequest.Proxy;

            if (myProxy != null)
            {
                myProxy.Credentials = CredentialCache.DefaultCredentials;
            }

            httpWebRequest.Method = "GET";

            HttpWebResponse res;

            res = (HttpWebResponse)httpWebRequest.GetResponse();

            HtmlDocument doc1 = new HtmlDocument();

            doc1.Load(res.GetResponseStream());

            HtmlNode node = doc1.DocumentNode.SelectSingleNode("//td[@class='mainbody']/table/tr[last()]/td");

            try
            {
                totalCompanies = node.InnerText;
                return totalCompanies;
            }
            catch (NullReferenceException e)
            {
                   totalCompanies = "No records found";
                    return totalCompanies;

             }




        }

You can use HttpWebRequest within your desktop app, i've done this before (winforms). 您可以在桌面应用程序中使用HttpWebRequest ,我之前已经这样做过(winforms)。 For example: - 例如: -

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url");
var response = new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();

You can then use HtmlAgilityPack to parse the data from the response: 然后,您可以使用HtmlAgilityPack从响应中解析数据:

 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
 doc.LoadHtml(response);

 //Sample query
 var node = doc.DocumentNode.Descendants("div")
           .Where(d => d.Attributes.Contains("id")).ToList(); 

(it would be helpful to include more details/be more specific) (包含更多详细信息/更具体会有所帮助)

If your ASP.NET page already does all the scraping, and all you need to do is access that ASP.NET page, you can simply use HttpWebRequest 如果您的ASP.NET页已经完成所有抓取操作,并且您需要做的就是访问该ASP.NET页,则只需使用HttpWebRequest

http://msdn.microsoft.com/en-us/library/456dfw4f.aspx - short description & tutorial http://msdn.microsoft.com/zh-CN/library/456dfw4f.aspx-简短说明和教程

If that URL is the website TO BE SCRAPED, and you need to include that ASP.NET script in your project, then you need to add it as a web service. 如果该URL是要废弃的网站,并且您需要在项目中包含该ASP.NET脚本,则需要将其添加为Web服务。

You can do it with both but also you can do it by adding a webbrowser to your desktop application. 您既可以做到这一点,也可以通过将Web浏览器添加到桌面应用程序来做到这一点。 I don't know why but result is much more faster. 我不知道为什么,但是结果要快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM