C#控制台/于网站服务器的访问 [英] C# Console/Server access to web site

查看:125
本文介绍了C#控制台/于网站服务器的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个C#项目,我需要从一个安全的网站,没有一个API或Web服务中获取数据。我的计划是要登录,得到我需要的页面,解析出HTML去的数据位我需要登录到数据库。现在我有一个控制台应用程序进行测试,但最终这将被转换为Azure的服务总线应用。

I am working on a C# project where I need to get data from a secured web site that does not have an API or web services. My plan is to login, get to the page I need, and parse out the HTML to get to the data bits I need to log to a database. Right now I'm testing with a console app, but eventually this will be converted to an Azure Service bus application.

在为了得到任何东西,你必须在他们login.cfm页面,这意味着我需要加载页面上的用户名和密码输入控件,并点击提交按钮登录。然后导航到我需要解析的页面。

In order to get to anything, you have to login at their login.cfm page, which means I need to load the username and password input controls on the page and click the submit button. Then navigate to the page I need to parse.

由于我没有'浏览器'来解析控件,我试图使用各种C#.NET类才能到页面中,设置用户名和密码,并点击提交,但似乎没有任何工作。

Since I don't have a 'browser' to parse for controls, I am trying to use various C# .NET classes to get to the page, set the username and password, and click submit, but nothing seems to work.

任何例子我可以看看,或.NET类我应该是被设计为这类项目在审查?

Any examples I can look at, or .NET classes I should be reviewing that were designed for this sort of project?

谢谢!

推荐答案

使用WebClient类中System.Net

Use the WebClient class in System.Net

有关会话cookie的持久性,你必须做出一个自定义的WebClient类。

For persistence of session cookie you'll have to make a custom WebClient class.

#region webclient with cookies
public class WebClientX : WebClient
{
    public CookieContainer cookies = new CookieContainer();
    protected override WebRequest GetWebRequest(Uri location)
    {
        WebRequest req = base.GetWebRequest(location);
        if (req is HttpWebRequest)
            (req as HttpWebRequest).CookieContainer = cookies;
        return req;
    }
    protected override WebResponse GetWebResponse(WebRequest request)
    {
        WebResponse res = base.GetWebResponse(request);
        if (res is HttpWebResponse)
            cookies.Add((res as HttpWebResponse).Cookies);
        return res;
    }
}
#endregion

使用一个浏览器插件,类似Firebug或内置到Chrome的开发工具来获得所发送的HTTP POST数据,当您提交表单。发送使用WebClientX类的职位和解析响应HTML。

Use a browser add-on like FireBug or the development tools built into Chrome to get the HTTP POST data being sent when you submit a form. Send those POSTs using the WebClientX class and parse the response HTML.

解析HTML最快的方法当你已经知道格式使用简单Regex.Match。所以你使用的开发工具来记录您的文章,URL和HTML内容,那么你将执行使用WebClientX相同的任务通过在浏览器的操作。

The fastest way to parse HTML when you already know the format is using a simple Regex.Match. So you'd go through the actions in your browser using the development tools to record your POSTs, URLs and HTML content then you'll perform the same tasks using the WebClientX.

这篇关于C#控制台/于网站服务器的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆