环境:node:v4.5.0 os:windows 7 64bit。
大家好,当我使用http向网站发布请求时,我收到了错误。
我搜索了许多答案并尝试了这些,但错误仍然存在。我能做什么? 这是错误消息:
_http_outgoing.js:351
throw new TypeError('The header content contains invalid characters');
TypeError: The header content contains invalid characters
at ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:351:13)
at new ClientRequest (_http_client.js:79:14)
at Object.exports.request (http.js:31:10)
at Object.<anonymous> (C:\Users\Administrator\http.js:30:16)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
这是我的代码:
var http = require('http');
var querystring = require('querystring');
var postData = querystring.stringify({
'content': 'abc',
'mid': 8837
});
var options = {
hostname: 'www.imooc.com',
port: 80,
path: '/course/docomment',
method: 'POST',
headers: {
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'en,zh-CN;q=0.8,zh;q=0.6',
'Connection':'keep-alive',
'Content-Length':postData.length,
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie':'imooc_uuid=e9896052-b2fb-4b23-85e8-74673c7ac165; imooc_isnew_ct=1476177989; loginstate=1; apsid=lhNTQ3NDk4OWFjZDBiNTFmODBjMmNkNDRmY2NjMmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjEzMzI0NAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAxMDI3MTkxNDkzQHFxLmNvbQAAAAAAAAAAAAAAAAAAADY1NTdiODMyZmM5NDMzZWNjYzg2ZGRjNDc5YzU3NTA2VrD8V1aw%2FFc%3DNz; last_login_username=1027191493%40qq.com; PHPSESSID=0ib2e90urbk47po8su89tvks13; IMCDNS=0; jwplayer.qualityLabel=è?????; jwplayer.volume=100; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1476346029,1476363725,1476416065,1476448867; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1476459900; imooc_isnew=2; cvde=5800d25c99f68-52',
'Host':'www.imooc.com',
'Origin':'http://www.imooc.com',
'Referer':'http://www.imooc.com/video/8837',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
}
var req = http.request(options, function(res) {
console.log('status:' + res.statusCode);
console.log('headers:' + JSON.stringify(res.headers));
res.on('data', function(chunk) {
console.log(Buffer.isBuffer(chunk));
console.log(typeof chunk);
});
res.on('end', function() {
console.log('评论完毕');
});
res.on('error', function(e) {
console.log('error:' + e.message);
});
})
req.write(postData);
req.end();
提前致谢!!!