Cookies Puppeter/puppetersharp:Cookie头覆盖是间歇性的

Cookies Puppeter/puppetersharp:Cookie头覆盖是间歇性的,cookies,puppeteer,puppeteer-sharp,chrome-devtools-protocol,Cookies,Puppeteer,Puppeteer Sharp,Chrome Devtools Protocol,我想覆盖网页中单个请求的cookie头。为此,我目前正在使用和Chrome Devtools协议API(特别是使用Fetch.enable,Fetch.requestPaused,以及Fetch.continueRequest) 请注意,我不想使用cookie,因为我想绕过浏览器对cookie存在的了解。(换句话说,我不希望cookie出现在Chrome开发工具->应用程序->cookie选项卡中) 我观察到这是间歇性的如何使此cookie覆盖不间断,而是应用于所有请求? 以下是木偶演员夏令营的

我想覆盖网页中单个请求的cookie头。为此,我目前正在使用和Chrome Devtools协议API(特别是使用
Fetch.enable
Fetch.requestPaused
,以及
Fetch.continueRequest

请注意,我不想使用cookie,因为我想绕过浏览器对cookie存在的了解。(换句话说,我不希望cookie出现在Chrome开发工具->应用程序->cookie选项卡中)

我观察到这是间歇性的如何使此cookie覆盖不间断,而是应用于所有请求?

以下是木偶演员夏令营的代码: (请参阅重要部分的
设置COOKIE头覆盖
注释)

类程序
{
公共静态CDP会话(U CDP会话);
公共静态无效CdpSessionMessageReceived(对象发送方,MessageEventArgs args args)
{
if(args.MessageID==“Fetch.requestPaused”)
{
Task.Run(异步()=>
{
FetchRequestPausedResponse fetchRequest=args.MessageData.ToObject();
列表标题=新列表();
添加(新字典)
{
{“名称”,“cookie”},
{“value”,“abcdefg=1234567”}//为每个请求设置COOKIE头覆盖
});
wait_cdpSession.SendAsync(“Fetch.continueRequest”),新字典
{
{“requestId”,fetchRequest.requestId},
{“headers”,headers.ToArray()}
}
);
});
}
}
静态void Main(字符串[]参数)
{
Browser b=await Puppeter.LaunchAsync(新的启动选项()
{
ExecutablePath=“C:\\ProgramFiles(x86)\\Google\\Chrome\\Application\\Chrome.exe”,
DefaultViewport=null,
无头=假
});
Page Page=b.PagesAsync()[0];
JObject response=await _cdpSession.SendAsync(“Fetch.enable”,
新词典
{
{
“模式”,新词典[]
{
新词典
{
{“urlPattern”,“*”},//URL模式是*来拦截每个请求
{“请求阶段”,“请求”}
}
}
}
});
_cdpSession.MessageReceived+=CdpSessionMessageReceived;
等待页面。GoToAsync(“https://www.google.com");
等待b.CloseAsync();
}
内部类FetchRequestPausedResponse
{
公共字符串RequestId{get;set;}
公共字符串NetworkId{get;set;}
}
}
如果我查看Chrome DevTools网络选项卡,我会发现21个请求中只有3个具有
abcdefg=1234567
cookie覆盖。以下是一个不符合以下条件的请求示例:


更新:在做更多的研究时,
onBeforeSendHeaders
确实可以完成我想要的功能,即覆盖单个请求的cookie头,而不告诉浏览器cookie是什么。是删除cookie头的代码示例(我找不到像上面的Puppeter代码片段那样替换cookie头的示例)

现在,我们来看看木偶艺人是如何做到发送头之前的
onBeforeSendHeaders
所做的

class Program
{
    public static CDPSession _cdpSession;

    public static void CdpSessionMessageReceived(object sender, MessageEventArgs args)
    {
        if (args.MessageID == "Fetch.requestPaused")
        {
            Task.Run(async () => 
            {
                FetchRequestPausedResponse fetchRequest = args.MessageData.ToObject<FetchRequestPausedResponse>();

                List<Dictionary<string, object>> headers = new List<Dictionary<string, object>>();
                headers.Add(new Dictionary<string, object>
                {
                    { "name", "cookie" },
                    { "value", "abcdefg=1234567" } // SET THE COOKIE HEADER OVERRIDE FOR EVERY REQUEST
                });

                await _cdpSession.SendAsync("Fetch.continueRequest", new Dictionary<string, object>
                    {
                        { "requestId", fetchRequest.RequestId },
                        { "headers", headers.ToArray() }
                    }
                );
            });
        }
    }

    static void Main(string[] args)
    {
        Browser b = await Puppeteer.LaunchAsync(new LaunchOptions()
        {
            ExecutablePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
            DefaultViewport = null,
            Headless = false
        });

        Page page = b.PagesAsync()[0];

        JObject response = await _cdpSession.SendAsync("Fetch.enable",
            new Dictionary<string, object>
            {
                {
                    "patterns", new Dictionary<string, object>[]
                    {
                        new Dictionary<string, object>
                        {
                            { "urlPattern", "*" }, // URL PATTERN IS * TO INTERCEPT EVERY REQUEST
                            { "requestStage", "Request" }
                        }
                    }
                }
            });

        _cdpSession.MessageReceived += CdpSessionMessageReceived;

        await page.GoToAsync("https://www.google.com");

        await b.CloseAsync();
    }

    internal class FetchRequestPausedResponse
    {
        public string RequestId { get; set; }

        public string NetworkId { get; set; }
    }
}