錯誤提示如下:
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();