req.on('response', function(res) { res.setEncoding('utf8'); var responseData = ''; var status = res.statusCode; var headers = res.headers; res.on('data', function(chunk) { responseData += chunk; }); res.on('end', function() { res.destroy(); if (headers['content-type'] && headers['content-type'].indexOf('application/json') !== -1) { try { responseData = JSON.parse(responseData); } catch (error) { return done(error); } } done(null, responseData, status, headers); }); });
// post数据到服务器 postData(pinCode) { let headers = { "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0", 'Accept-Language': 'zh-cn', 'Content-Type': 'application/x-www-form-urlencoded', 'Connection': 'Keep-Alive' }; let encodeBody = encodePostData.encodePostData(this.userName, this.userPwd, this.preLoginData.servertime, this.preLoginData.nonce, this.preLoginData.pubkey, this.preLoginData.rsakv, this.pinCode, this.preLoginData['pcid']); let options = { method: 'POST', url: this.loginUrl, headers: headers, body: encodeBody, gzip: true }; return new Promise((resolve, reject) => { request(options, (error, response, body) => { if (!error && response.statusCode == 200) { response.setEncoding('utf-8'); resolve(response.body); } else { reject(response.body); } }); }); }
req.on('response', function (res) { var body = '' res.setEncoding('utf8') res.on('data', function (data) { body += data
req.on('response', function (res) { var body = '' res.setEncoding('utf8') res.on('data', function (data) { body += data
.setEncoding('utf8') .pipe(response) .on('error', (er) => {
// post数据到服务器 postData(pinCode) { let headers = { "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0", 'Accept-Language': 'zh-cn', 'Content-Type':'application/x-www-form-urlencoded', 'Connection': 'Keep-Alive' }; let encodeBody = encodePostData.encodePostData(this.userName, this.userPwd, this.preLoginData.servertime, this.preLoginData.nonce, this.preLoginData.pubkey, this.preLoginData.rsakv, this.pinCode, this.preLoginData['pcid']); let options = { method: 'POST', url: this.loginUrl, headers: headers, body: encodeBody, gzip: true }; return new Promise((resolve, reject) => { request(options, (error, response, body) => { if (!error && response.statusCode == 200) { response.setEncoding('utf-8'); resolve(response.body); }else{ reject(response.body) } }) }) }