同一请求,用python请求正常,但是用node请求报500

const https = require('https');

patchUrl = 'https://***.***.com/biz/visitorCard/download?data=%7B%22beginTime%22%3A%222020-07-05T16%3A00%3A00.000Z%22%2C%22endTime%22%3A%222023-08-19T15%3A59%3A59.000Z%22%2C%22pageNum%22%3A1%2C%22pageSize%22%3A10%2C%22queryList%22%3A%5B%5D%2C%22allTag%22%3A0%2C%22sortField%22%3A%22%22%2C%22sortType%22%3A%22%22%7D&token=eyJhbGciOiJIUzUxMiJ9.eyJziY3JlYXRlZCI6MTY4OTiOjYwMX0.ptUE18_j_jAEPN8zNt0eiq0MrIlExxM9QY5UP72atWiuxlPrldCDqDXzWqV3kSTyhPdrqI8eXD46FRnzCWhaJQ'


https.get(patchUrl, (response) => {
    console.info("code", response.statusCode);
}).on('error', (err) => {
    console.info('download-error', 'Failed to connect to the server.');
});
import requests


patchUrl = 'https://***.***.com/biz/visitorCard/download?data=%7B%22beginTime%22%3A%222020-07-05T16%3A00%3A00.000Z%22%2C%22endTime%22%3A%222023-08-19T15%3A59%3A59.000Z%22%2C%22pageNum%22%3A1%2C%22pageSize%22%3A10%2C%22queryList%22%3A%5B%5D%2C%22allTag%22%3A0%2C%22sortField%22%3A%22%22%2C%22sortType%22%3A%22%22%7D&token=eyJhbGciOiJIUzUxMiJ9.eyJziY3JlYXRlZCI6MTY4OTiOjYwMX0.ptUE18_j_jAEPN8zNt0eiq0MrIlExxM9QY5UP72atWiuxlPrldCDqDXzWqV3kSTyhPdrqI8eXD46FRnzCWhaJQ'

res = requests.get(patchUrl)

print(res.status_code)


为啥会出现这种情况呢?通过层层分析,最后得出这两种语言对headers设定的默认值不一样,nodejs中添加user-agent就正常运行了


https.get(patchUrl, {
    headers: {
        "User-Agent": "Mozilla/5.0 (Linux; Android 10; Redmi K30 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Mobile Safari/537.36",
    }
}, (response) => {
    
    console.info("code", response.statusCode);
}).on('error', (err) => {
    console.info('download-error', 'Failed to connect to the server.');
});

猜你喜欢

转载自blog.csdn.net/cuiyuchen111/article/details/131838295