错误提示如下:
Error: write EPROTO 140052867975040:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:827:
at _errnoException (util.js:1022:11)
at WriteWrap.afterWrite [as oncomplete] (net.js:880:14) code: 'EPROTO', errno: 'EPROTO', syscall: 'write' }
最后发现是因为https使用的端口是443,而http使用的端口是80,这里要切换过来
代码如下:
var content = querystring.stringify("id=111111");
var options = {
host: '127.0.0.1',
hostname: "www.baidu.com",
//port: 80, //http
port: 443, //https
path: action,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'/* ,
'Content-Length': send.length */
}
};
var req = http.request(options, function (res) {
//返回数据流
var body="";
res.setEncoding('utf8');
res.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
body += chunk;
});
res.on('end', function(){
// console.log("body = "+body);
if(body.length>0){
try{
//逻辑处理
} catch(e){
console.log(e);
}
}
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.write(content);
req.end();