繞過Referer和Host檢查


1、我們在嘗試抓取其他網站的數據接口時,某些接口需要經過請求頭中的Host和Referer的檢查,不是指定的host或referer將不予返回數據,且前端無法繞過這種檢查

此時通過后端代理解決

在vue-cli 環境下,以qq音樂為例偽造請求頭:

 

  1.1 打開配置文件webpack.dev.conf,js,安裝express,axios

並在文件開頭const portfinder = require('portfinder')后進行引入

const axios = require('axios')
const express = require('express')

let app = express()
let apiRoutes = express.Router()
app.use('/api', apiRoutes)

  1.2 找到devServer節點,在其中配置新增before函數調用,在函數體內使用axios代為發送請求,前端的請求將請求到這里
before(app) {
app.get('/api/getDiscList', (req, res) => {
const url = "https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg"
axios.get(url, {
headers: {
referer: "https://y.qq.com/m/index.html",
host:'c.y.qq.com'
},
params: req.query//轉發前端請求的參數
}).then((response) => {
res.json(response.data)//傳回qq后台相應的請求至前端
}).catch((err) => {
console.log(err)
})

})
}

 

 
 1,.3 修改前端請求地址
export function getDiscList() {
  // 使用jsonp抓取數據
  const url = '/api/getDiscList'

  const data = Object.assign({}, commonParams, {
    _: '1557234847616',
    platform: 'yqq',
    hostUinL: 0,
    sin: 0,
    ein: 29,
    sortId: 5,
    needNewCode: 0,
    categoryId: 10000000,
    rnd: Math.random(),
    format: 'json'
  })

  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM