此时客户端302重定向拦截成功。
但是下一次拦截就不成功了。
我们显然发现,这个重定向请求没有在客户端拦截,而是自动发送给了服务器,但是没带上保持登陆状态的cookie,所以404。
下面是发现该错误的代码片段:
function get_redirect_location_from_jwgl(url, header, postData){
return new Promise((resolve) => {
wx.request({
url,
method: 'POST',
data: postData,
timeout: 20000,
header,
redirect: 'manual',
}).onHeadersReceived((backmsg) => {
// 获取backmsg
return resolve(backmsg.header.Location)
})
})
}
async function Get_Redirect_Location(url, header, postData) {
const redirect_location = await get_redirect_location_from_jwgl(url, header, postData)
return redirect_location
}
function get_new_jsessionid(url, header){
return new Promise((resolve) => {
wx.request({
url,
method: 'GET',
timeout: 20000,
header,
redirect: 'manual'
}).onHeadersReceived((res) => {
return resolve(res.cookies)
})
})
}
async function Get_New_JSESSIONID(url, header){
const new_jsessionid = await get_new_jsessionid(url, header)
return new_jsessionid
}
export function submit_form(url, header, postData, TOKEN) {
// 提交表单,获取最新JSESSIONID
Get_Redirect_Location(url, header, postData).then((Redirection) => {
Get_New_JSESSIONID(Redirection, header).then((res) =>{
console.log(res)
})
})
我已经排除了可能由于函数嵌套产生该问题,就算把Get_New_JSESSIONID函数拿出来,也没办法解决这个问题。
临时解决方案:使用python requests库代替wx.request,构建外网fastapi。
工具:一个服务器,一个已备案的域名。
FastAPI构建代码:
# fastapi_get_jessionid.py from subprocess import Popen from typing import Optional import uvicorn from fastapi import FastAPI, Request, Cookie, Header from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse import requests app = FastAPI() @app.exception_handler(RequestValidationError) async def request_validation_exception_handler(request: Request, exc: RequestValidationError): # 可以用日志记录请求信息,方便排错 return JSONResponse({"code": "404", "message": "非法访问"}) @app.get("/jsessionid") def get_jsessionid(request: Request, ticket: str): token = request.headers.get('cookie') # Cookie检验 if (ticket is None or token is None or type(token) is not str or 'route' not in token or '; JSESSIONID' not in token): return JSONResponse({"code": 404, "message": "非法访问"}) url = 'https://jwgl.bupt.edu.cn/jsxsd/xk/LoginToXk?method=jwxt&ticqzket=' + ticket headers = {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, ' 'like Gecko) Version/15.0 Mobile/15E148 Safari/604.1 wechatdevtools/1.06.2401020 ' 'MicroMessenger/8.0.5 webview/', 'Referer': 'https://jwgl.bupt.edu.cn/', 'Host': 'jwgl.bupt.edu.cn', 'cookie': token } response = requests.get(url, headers=headers, allow_redirects=False) return JSONResponse({'code': 200, 'cookie': response.headers['Set-Cookie'].split(';')[0]}) # 启动uvicorn服务,默认端口443,fastapi_get_jsessionid对应文件名 if __name__ == '__main__': Popen(['python', '-m', 'https_redirect']) # HTTP Request will redirect to HTTPS, forcing to use HTTPS Request uvicorn.run('fastapi_get_jsessionid:app', port=443, host='0.0.0.0', reload=True, ssl_keyfile='private.key', ssl_certfile='certificate.pem')
# https_redirect.py import uvicorn from fastapi import FastAPI from starlette.requests import Request from starlette.responses import RedirectResponse app = FastAPI() @app.route('/{_:path}') async def https_redirect(request: Request): return RedirectResponse(request.url.replace(scheme='https')) if __name__ == '__main__': uvicorn.run('https_redirect:app', port=80, host='0.0.0.0')
示例访问网址:https://fastapi.jerrychat.cn/jsessionid?ticket={your ticket}
请求头中的cookie = {your cookie}
cookie格式:route={your route}; JSESSIONID={your JSESSIONID}
再使用wx.request访问上述网址获取最新的cookie信息。