下面是演示代码,运行以下代码打开论坛以后,请输入用户名、密码登录论坛,然后点击“后台抓网页”按钮在 aardio中调用 inet.http 抓取网页,注意这时候抓取到的页面显示的是登录后的页面,可以看到 inet.http 与 web.kit 成功共享会话。
注意web.kit库要先升级到最新版
mport win.ui; /*DSG{{*/ var winform = win.form(text="web.kit/inet.http 共享会话 - 登录后再试";right=1206;bottom=795) winform.add( btnHttpGet={cls="button";text="后台抓取网页";left=1079;top=16;right=1183;bottom=60;dr=1;dt=1;z=1}; btnOpen={cls="button";text="打开网页";left=961;top=15;right=1062;bottom=57;dr=1;dt=1;z=5}; custom={cls="custom";text="custom";left=36;top=88;right=1173;bottom=766;db=1;dl=1;dr=1;dt=1;z=3}; edit={cls="edit";left=28;top=76;right=1190;bottom=765;db=1;dl=1;dr=1;dt=1;edge=1;hide=1;multiline=1;z=4}; editUrl={cls="edit";text="http://bbs.aardio.com";left=28;top=21;right=956;bottom=50;dl=1;dr=1;dt=1;edge=1;multiline=1;z=2} ) /*}}*/ import web.kit.form; var wb = web.kit.form( winform.custom ); winform.show(); //打开网页 wb.go("http://bbs.aardio.com/") //自动点击登录页面 wb.waitQueryEles("a",{innerText="登录";href="login"} ).click(); import string.html; import inet.http; winform.btnHttpGet.oncommand = function(id,event){ //首先取浏览器的userAgent,假装是浏览器 var http = inet.http(wb.script.navigator.userAgent); //禁用默认的cookie管理 http.disableCache(); http.disableCookies(); //把浏览器的cookie加上去 http.addHeaders ={cookie = wb.cookie} var str = http.get(wb.location); if( str ){ if(!string.isUtf8(str)) str = string.fromto(str,0,65001); winform.edit.text = string.html.toText(str); } else { winform.edit.text = ""; } winform.custom.hide = true; winform.edit.hide = false; } winform.btnOpen.oncommand = function(id,event){ wb.go( winform.editUrl.text ); winform.custom.hide = false; winform.edit.hide = true; } wb.onURLChanged = function(url){ winform.editUrl.text = url; } //启动消息循环 win.loopMessage();
import win.ui; /*DSG{{*/ var winform = win.form(text="webkit 获取所有网站cookie") /*}}*/ import web.kit.form; var wb = web.kit.form( winform ); wb.go("http://www.baidu.com") winform.show(); win.delay(1000); //写网站cookie wb.cookieSet( name = "test"; value = "abc"; domain = ".baidu.com"; path = "/"; expires = time().addday(30) ) //输出所有网站cookie winform.msgbox( wb.cookieData().stringify() ) //转换为HTTP请求头格式 var cookies = wb.cookieData().getCookies("baidu.com") //首先取浏览器的userAgent,假装是浏览器 import inet.http; var http = inet.http(wb.script.navigator.userAgent); //禁用默认的cookie管理 http.disableCache(); http.disableCookies(); //把浏览器的cookie加上去 http.addHeaders ={cookie = cookies } //跟浏览器共话 var str = http.get(wb.location); win.loopMessage();
转帖于:http://bbs.aardio.com/forum.php?mod=viewthread&tid=22023&extra=page%3D1