Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中处理HTTP响应的框架#_C#_Httpresponse_Anglesharp - Fatal编程技术网

C# 在C语言中处理HTTP响应的框架#

C# 在C语言中处理HTTP响应的框架#,c#,httpresponse,anglesharp,C#,Httpresponse,Anglesharp,该项目是一个与网页交互的C#桌面应用程序 上次我做这样的事情时,我使用了WatiN和HTMLAgilityPack。但是WatiN并不是很优雅,因为它会打开一个浏览器窗口与网站进行交互。它更多地是为集成测试而设计的,但它仍然完成了工作 这一次,我在看AngleSharp解析HTML,但我仍然需要编写代码登录到网站,按下几个按钮并发布一些帖子 有没有什么框架可以让这变得简单?如果你想与网站互动,填写文本框,点击按钮等,我认为一个更合理的解决方案是使用和管理一个实际的web浏览器 嗯-看起来我低

该项目是一个与网页交互的C#桌面应用程序

上次我做这样的事情时,我使用了WatiN和HTMLAgilityPack。但是WatiN并不是很优雅,因为它会打开一个浏览器窗口与网站进行交互。它更多地是为集成测试而设计的,但它仍然完成了工作

这一次,我在看AngleSharp解析HTML,但我仍然需要编写代码登录到网站,按下几个按钮并发布一些帖子


有没有什么框架可以让这变得简单?

如果你想与网站互动,填写文本框,点击按钮等,我认为一个更合理的解决方案是使用和管理一个实际的web浏览器


嗯-看起来我低估了AngleSharp的力量

有一篇精彩的文章描述了如何使用它登录网站,并发布表单

这个库已经更新了,所以有些东西已经改变了,但是功能和方法是一样的。 我将在这里包括我的“测试”代码,它演示了可用性

public async Task LogIn()
        {
            //Sets up the context to preserve state from one request to the next
            var configuration = Configuration.Default.WithDefaultLoader().WithDefaultCookies();
            var context = BrowsingContext.New(configuration);

            /Loads the login page   
            await context.OpenAsync("https://my.website.com/login/");

            //Identifies the only form on the page (can use CSS selectors to choose one if multiple), fills in the fields and submits
            await context.Active.QuerySelector<IHtmlFormElement>("form").SubmitAsync(new
            {
                username = "CharlieChaplin",
                pass = "x78gjdngmf"
            });
            
            //stores the response page body in the result variable.   
            var result = context.Active.Body;
公共异步任务登录()
{
//设置上下文以保留从一个请求到下一个请求的状态
var configuration=configuration.Default.WithDefaultLoader().WithDefaultCookies();
var context=BrowsingContext.New(配置);
/加载登录页面
等待上下文。OpenAsync(“https://my.website.com/login/");
//标识页面上的唯一表单(如果有多个表单,可以使用CSS选择器选择一个),填写字段并提交
wait context.Active.QuerySelector(“表单”).SubmitAsync(新
{
username=“charlichthaplin”,
pass=“x78gjdngmf”
});
//将响应页面正文存储在结果变量中。
var result=context.Active.Body;
编辑-在使用这个工具一段时间后,我发现Anglesharp.IO中有一个更健壮的httprequest

public async Task LogIn()
        {
            var client = new HttpClient();
            var requester = new HttpClientRequester(client);
            //Sets up the context to preserve state from one request to the next
            var configuration = Configuration.Default
                               .WithRequester(requester)
                               .WithDefaultLoader()
                               .WithDefaultCookies();

            var context = BrowsingContext.New(configuration);

            /Loads the login page   
            await context.OpenAsync("https://my.website.com/login/");

            //Identifies the only form on the page (can use CSS selectors to choose one if multiple), fills in the fields and submits
            await context.Active.QuerySelector<IHtmlFormElement>("form").SubmitAsync(new
            {
                username = "CharlieChaplin",
                pass = "x78gjdngmf"
            });
公共异步任务登录()
{
var client=新的HttpClient();
var请求者=新的HttpClientRequester(客户端);
//设置上下文以保留从一个请求到下一个请求的状态
var configuration=configuration.Default
.WithRequester(请求者)
.WithDefaultLoader()
.WithDefaultCookies();
var context=BrowsingContext.New(配置);
/加载登录页面
等待上下文。OpenAsync(“https://my.website.com/login/");
//标识页面上的唯一表单(如果有多个表单,可以使用CSS选择器选择一个),填写字段并提交
wait context.Active.QuerySelector(“表单”).SubmitAsync(新
{
username=“charlichthaplin”,
pass=“x78gjdngmf”
});

这是一个非常愚蠢的问题,但您是否有可能与web服务交互(例如REST API)而不是一个网站?不确定这是否是任务的正确位置,但是:?@Arcord-不-那很容易。这是一个我无法控制的网页。@Musterknabe-这不是我想要的,但是谢谢。使用HttpWebRequest和HttpWebresponse没有视图(像浏览器视图)因此,它比网页更快。HttpWebRequest不会自动添加许多自动添加到浏览器中的标题,因此您通常必须添加缺少的标题。WatiN的工作方式如上所述。我问了这个问题,因为这次我不想使用浏览器。