aardio 文档

aardio 范例: WebView2 - CDP HTTP 自动认证

//HTTP 自动认证,适用 HTTP 代理服务器
import win.ui;
import web.view;
/*DSG{{*/
var winform = win.form(text="WebView2 - CDP HTTP 自动认证";right=966;bottom=622)
winform.add()
/*}}*/

var wb = web.view(winform);
winform.show();

// 1. 启用网络请求拦截 (关键步骤)
wb.cdp("Network.setRequestInterception", {
    patterns = {
        {urlPattern = "*"} // 拦截所有 URL 请求
    }
});

// 2. 订阅 Network.requestIntercepted 事件
wb.cdpSubscribe("Network.requestIntercepted", function(params) {

    //返回异步执行函数对象,不要阻塞事件
    return function(){ 

        // 检查浏览器是否发来了认证请求 (authChallenge)
        if (params.authChallenge) {

            // 我们继续请求,并附上凭据
            var result =  wb.cdp("Network.continueInterceptedRequest", {
                interceptionId = params.interceptionId;
                authChallengeResponse = {
                    response = "ProvideCredentials"; // 响应类型:提供凭据
                    username = "username"; //HTTP 认证用户名
                    password = "password"; //HTTP 认证录密码
                }
            }); 
        }
        else {

            // 如果没有认证挑战,说明是普通请求,直接放行
            wb.cdp("Network.continueInterceptedRequest", {
                interceptionId = params.interceptionId;
            });
        }   
    }
});

// 导航到一个网页来测试代理
wb.go("https://httpbin.org/basic-auth/username/password");

win.loopMessage();
Markdown 格式