代码拉取完成,页面将自动刷新
const URL = require('url')
const http = require('http')
const zlib = require('zlib')
const querystring = require('querystring')
const proxy = {}
proxy.GET = function(url, headers) {
return request(false, url, headers)
}
proxy.POST = function(url, headers, form) {
return request(true, url, headers, form)
}
function request(isPOST, url, headers, form) {
return new Promise((revlose, reject)=>{
headers = headers || {}
form = form || {}
let postData = querystring.stringify(form)
let options = URL.parse(url)
options.method = isPOST ? 'POST' : 'GET'
options.headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7,zh-TW;q=0.6',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Host': options.host,
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
}
if (isPOST) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded'
options.headers['Content-Length'] = postData.length
}
for (let k in headers) {
options.headers[k] = headers[k]
}
let req = http.request(options, (res) => {
res.setTimeout(3000)
let data = []
res.on('data', (chunk) => {
data.push(chunk)
})
res.on('end', () => {
let body = data.join('')
if (res.headers['content-encoding'] == 'gzip') { // Gzip支持
zlib.gunzip(Buffer.concat(data), function(err, decoded) {
if (err) {
revlose({ code: res.statusCode, headers: res.headers, body: body })
} else {
revlose({ code: res.statusCode, headers: res.headers, body: decoded.toString() })
}
})
} else {
revlose({ code: res.statusCode, headers: res.headers, body: body })
}
})
})
req.setTimeout(3000)
req.on('error', (err) => {
reject(err)
})
if (isPOST) {
req.write(postData)
}
req.end()
})
}
module.exports = proxy
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。