简体   繁体   English

在C#中添加Httpheader到selenium chrome webdriver

[英]Add Httpheader to selenium chrome webdriver in C#

My C# code looks like this for creating chrome web driver, i wanted to add the custom HTTP headers to all my http requests. 我的C#代码看起来像是用于创建chrome web驱动程序,我想将自定义HTTP标头添加到我的所有http请求中。

ex: user-agent : Android 例如:用户代理:Android

var service = ChromeDriverService.CreateDefaultService(@"c:\Chrome\");
var option = new ChromeOptions();
_driver = new ChromeDriver(service, option);

We have the way for firefox, as the link shows, but for chrome it does not work. 我们有firefox的方式,如链接所示,但对于chrome它不起作用。 https://eveningsamurai.wordpress.com/2013/11/21/changing-http-headers-for-a-selenium-webdriver-request/ https://eveningsamurai.wordpress.com/2013/11/21/changing-http-headers-for-a-selenium-webdriver-request/

Any help appreciated 任何帮助赞赏

I'm using the approach using ModHeaders. 我正在使用ModHeaders的方法。 Previously you should download the mod headers plugin for instance with this plugin . 以前你应该使用这个插件下载mod头插件 Then you can add it, be carefully referencing it from your source code. 然后您可以添加它,从源代码中仔细引用它。

var options = new ChromeOptions();
options.AddExtension("WebDrivers/modHeader_2_1_1.crx");

var driver = new ChromeDriver(options);

Then I have only found this kind of a hack to access this plugin 然后我才发现这种黑客访问这个插件

// set the context to access extension local storage
Configuration.driver.Navigate().GoToUrl("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png");

Configuration.driver.ExecuteScript(
    "localStorage.setItem('profiles', JSON.stringify([{                " +
    "  title: 'Selenium', hideComment: true, appendMode: '',           " +
    "  headers: [                                                      " +
    "    {enabled: true, name: 'MY_HEADER', value: 'MY_VALUE', comment: ''}  " +
    "  ],                                                              " +
    "  respHeaders: [],                                                " +
    "  filters: []                                                     " +
    "}]));                                                             ");

And finally you can navigate to somewhere and check that the headers are loaded 最后,您可以导航到某个位置并检查标头是否已加载

    Configuration.driver.Navigate().GoToUrl("http://example.com/");

One way to handle this case is with FiddlerCore proxy, capture all the requests and modify the header as part of request. 处理这种情况的一种方法是使用FiddlerCore代理,捕获所有请求并在请求中修改标头。 https://www.nuget.org/packages/FiddlerCore/ https://www.nuget.org/packages/FiddlerCore/

Nice blog about fiddler core http://weblog.west-wind.com/posts/2014/Jul/29/Using-FiddlerCore-to-capture-HTTP-Requests-with-NET 关于fiddler核心的好博客http://weblog.west-wind.com/posts/2014/Jul/29/Using-FiddlerCore-to-capture-HTTP-Requests-with-NET

    public static void Start()
    {
        FiddlerApplication.RequestHeadersAvailable += FiddlerApplication_RequestHeadersAvailable;
        FiddlerApplication.Startup(8888, true, true, true);
    }

    static void FiddlerApplication_RequestHeadersAvailable(Session oSession)
    {
        oSession.RequestHeaders.Add("My_Custom_Header", "XXXXXXXXXXXXXXXX");
    }

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

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