downloadfile 图片请求 但是为什么返回的是text/html格式?
发布于 3 年前 作者 hzhao 11878 次浏览 来自 问答
Page({
  data: {
    header: {},
    verifycode'',
    img'',
  },

  onLoadfunction() {
    var _this = this;
    var str = 'http://jwgl.thxy.edu.cn/yzm?d' + new Date().getTime();
    console.log('时间戳为:',new Date().getTime());

    wx.downloadFile({
      url: str,
      successfunction(res{
        console.log(res);
        _this.setData({
          header: {
            'cookie': res.header['Set-Cookie'],
            'content-type''application/x-www-form-urlencoded;charset=UTF-8',
            'User-Agent''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36 Edg/96.0.1054.29'
          }
        });
        _this.setData({
          img: res.tempFilePath
        })
      }
    })
    
  },
5 回复

你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html

检验登录了,放开就行了

给你一个 nodejs 版的思路,获取到browserID=1的cookie,对应验证码的base64和cookie传给前端,前端输入账号、密码和验证码,后台进行模拟登录。* browserID 对应一个用户,随机生成并存储 *
// 获取验证码
app.get("/getCaptchaData", async (req, res, next) => {
    let Request = require("request-promise")
    let jar = Request.jar()
    let Http = Request.defaults({
        jar,
        pool: {
            maxSockets: 5000
        }
    })
    const baseUrl = `http://jwgl.thxy.edu.cn`

    Http({
        uri: baseUrl,
        resolveWithFullResponse: false
    }).then(() => {
        let cookie = jar.getCookieString(baseUrl) + "; browserID=1"

        Http({
            uri: `${baseUrl}/yzm?d=${Date.now()}`,
            encoding : null,
            headers: {
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                "content-type": "image/jpeg",
                cookie
            }
        }).then(buffer => {
            let base64 = `data:image/jpeg;base64,${Buffer.from(buffer, "utf-8").toString("base64")}`
            res.json({
                errcode: 0,
                errmsg: "ok",
                data: {
                    cookie,
                    base64
                }
            })
        }).catch(() => {
            res.json({
                errcode: -1,
                errmsg: "系统异常,请稍候再试",
                data: null
            })
        })
    }).catch(() => {
        res.json({
            errcode: -1,
            errmsg: "系统异常,请稍候再试",
            data: null
        })
    })
})

// 模拟登陆
app.get("/login", async (req, res, next) => {
    let Request = require("request-promise")
    let jar = Request.jar()
    let Http = Request.defaults({
        jar,
        pool: {
            maxSockets: 5000
        }
    })
    const Base64 = require("./base64") // 该文件链接http://jwgl.thxy.edu.cn/styles/js/export/base64.js,下载下来后添加一句 module.exports = Base64
    let formData = req.query //前端传cookie和登录信息account、pwd、verifycode
    formData.pwd = Base64.encode(formData.pwd)

    Http({
        uri: `http://jwgl.thxy.edu.cn/login!doLogin.action`,
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            cookie: formData.cookie
        },
        formData,
        resolveWithFullResponse: true
    }).then(body => {
        res.json({
            errcode: 0,
            errmsg: "ok",
            formData,
            data: body
        })
    }).catch(() => {
        res.json({
            errcode: -1,
            errmsg: "系统异常,请稍候再试",
            data: null
        })
    })
})
回到顶部