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 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短