WebRequest不返回HTML [英] WebRequest not returning HTML

查看:95
本文介绍了WebRequest不返回HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要加载此 http://www.yellowpages.ae/Categories-by-alphabet/h.html 网址,但返回null

I want to load this http://www.yellowpages.ae/categories-by-alphabet/h.html url, but it returns null

在某些问题上,我听说过要添加Cookie容器,但是我的代码中已经包含了它.

In some question I have heard about adding Cookie container but it is already there in my code.

var MainUrl = "http://www.yellowpages.ae/categories-by-alphabet/h.html";
HtmlWeb web = new HtmlWeb();
web.PreRequest += request =>
{
    request.CookieContainer = new System.Net.CookieContainer();
    return true;
};
web.CacheOnly = false;
var doc = web.Load(MainUrl);

该网站可以在浏览器中完美打开.

the website opens perfectly fine in browser.

推荐答案

您需要CookieCollection来获取cookie并将UseCookie设置为HtmlWeb中的true.

You need CookieCollection to get cookies and set UseCookie to true in HtmlWeb.

CookieCollection cookieCollection = null;
var web = new HtmlWeb
{
    //AutoDetectEncoding = true,
    UseCookies = true,
    CacheOnly = false,
    PreRequest = request =>
    {
        if (cookieCollection != null && cookieCollection.Count > 0)
            request.CookieContainer.Add(cookieCollection);

        return true;
    },
    PostResponse = (request, response) => { cookieCollection = response.Cookies; }
};

var doc = web.Load("https://www.google.com");

这篇关于WebRequest不返回HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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