aardio自定义cookie的http请求
By
popy32
at 2021-10-15 • 0人收藏 • 1555人看过
一直看见有同学问aardio怎么自定义cookie请求。看了inet的源码和文档后总结如下,测试API时最常见的自定义cookies和自定义header
import console; import inet.http; var http = inet.http(); // 自定义Header http.addHeaders = "Authorization: Bearer 057a3316-4817-4f43-8451-8ec66d06ff1f"; // 自定义Cookies 注意key和value在参数里是反的 // inet.http和IE共享cookies inet.clearCookie(); inet.setCookie("https://httpbin.org", "057a3316-4817-4f43-8451-8ec66d06ff1f", "Admin-Token" ); inet.setCookie("https://httpbin.org", "43200", "Admin-Expires-In" ); var html,err,errCode = http.get( "https://httpbin.org/anything" ); console.log(html) console.pause(true);
3 个回复 | 最后更新于 2023-07-28
可以用inet.setCookie()预置cookie。之后inet.http会自动引用这里的cookie,同时具备自动管理cookie的能力。这样解决需要人工干预登录,又批量管理账号的情况。
import console import inet import web.rest.jsonLiteClient; var domain = "https://buff.163.com/"; var cookies = "Device-Id=bh3Tm09wfBTEdlmWD5fx1; P_INFO=130262577132|1690377711|0|netease_buff|00&99|null&null&null#gud&440300#10#0|&0||13026257713; remember_me=U1095280909|vMhA64u6ceJNzhfCdMPoA1ngW4BxdUU9; session=1-vj9SuXJQDLq3EMWdbTfXj6Z4g8p9nwV5z4y3yIZQ3xFc2038838869; Locale-Supported=zh-Hans; game=csgo; csrf_token=IjY5ZTI2MmQ5M2Y2MTM1ODIyZTI3M2E3MWMzYzljN2M1ZTUwOGYwZTAi.F6P6ug.1u-ZOv7UVuwWK_H-zuPkMEWBA"; // 设置默认cookie,解决登录问题 for m in string.gmatch(cookies ,"(\S+?);") { inet.setCookie(domain,m); } //console.log(inet.getCookie("http://buff.163.com/")) var http = web.rest.jsonLiteClient(); // 自动设置header的token,将返回的cookie设置为下一次请求的token http._http.onReceiveBegin = function(statusCode,contentLength){ var cookies = http._http.readHeaderList("set-cookie"); console.dumpTable(cookies); for(k,v in cookies["set-cookie"]){ var key,value = string.match(v,"^(\S+)=([^;]+)"); if(key == "csrf_token"){ http.setHeaders({"x-csrftoken": value}) } } //console.log(inet.getCookie(domain)) } var api = http.api(domain);
登录后方可回帖
其实不用那么麻烦
只要禁用 cookie 的自动添加管理就行了
http.disableCookies()
然后在请求头里添加就行了
大概这个样子