Nodejs Post请求报socket hang up错误的解决办法
参考nodejs官网发送httppost请求的方法,实现了一个模拟post提交的功能。实际使用时报sockethangup错误。
后来发现是请求头设置的问题,发送选项中需要加上headers字段信息(这个估计也和对方的服务器有关,对于不完成的post请求头,可能被丢弃了)。
完整的代码如下(遇到类型问题的同学可以做个参考):
varquerystring=require('querystring')
,http=require('http');
vardata=querystring.stringify({ info:'hi', test:5 });
varopt={ hostname:'www.test.com', port:9094, path:'/perationSqlQuery', method:'POST', headers:{ 'Content-Type':'application/x-www-form-urlencoded', 'Content-Length':data.length } };
varreq=http.request(opt,function(res){ res.on('data',function(data){ console.log(data.toString()); }); }); req.on('error',function(e){ console.log('problemwithrequest:'+e.message); }); req.write(data); req.end();
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短