nodejs+获取html乱码,nodejs http.request 获得的数据中文乱码

本地创建了个客户端,用来post请求java 的一个数据接口,发现获得的数据中中文部分都是乱码

app.js 完整代码如下:

const Koa = require('koa');

const request = require('request-promise');

const static = require('koa-static');

const path = require('path');

const http = require('http');

const qs = require('querystring');

const Router = require('koa-router');

const app = new Koa();

const port = 9010;

const router = new Router();

app.use(static(path.resolve(__dirname), {

// Try to serve the gzipped version of a file automatically

gzip: true,

// Browser cache max-age in milliseconds.defaults to 0

maxage: 1000 * 3600,

// Allow transfer of hidden files. defaults to false

hidden: false

}));

router.get('/', async (ctx, next)=>{

const data = qs.stringify({

query: 1,

transactionid: 123456,

button1: '+%C8%B7+%B6%A8+'

});

const options = {

protocol : 'http:',

host: "202.109.79.211",

port: 8002,

method: 'POST',

path: '/TransFlowQueryResult.jsp',

headers: {

'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',

'Content-Length': Buffer.byteLength(data)

}

};

const body = await fetch(options, data);

ctx.type = 'html';

ctx.body = body;

});

app.use(router.routes(), router.allowedMethods());

app.listen(port, ()=>{

console.log('house.guazipu.cn wake up: %s', 'http://localhost:' + port);

});

function fetch(options = {}, payload = {}) {

return new Promise((resolve, reject) => {

const client = http.request(options, (res) => {

res.setEncoding('utf8');

res.on('data', (chunk) => {

resolve(chunk);

})

res.on('end', () => {

console.log('data transfer end');

})

});

client.on('error', (err) => {

reject(err);

});

client.write(payload);

client.end();

});

}

package.json

{

"name": "house",

"version": "1.0.0",

"description": "",

"main": "app.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"keywords": [],

"license": "ISC",

"dependencies": {

"iconv-lite": "^0.4.19",

"koa": "^2.3.0",

"koa-router": "^7.2.1",

"koa-static": "^4.0.1",

"request": "^2.83.0",

"request-promise": "^4.2.2"

}

}

最终效果:

bVXM1l?w=1234&h=136

打了断点,查看数据响应

bVXM9N?w=996&h=330

bVXM9U?w=872&h=520

接口返回的数据是 GBK

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值