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();
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短