【已解决】问一个有关于GB2312的Nodejs问题

【已解决】问一个有关于GB2312的Nodejs问题

#####-----------------------------vvvv解决方案在最下方

我在做我们学校教务网爬虫的时候,遇到了一个奇葩无比的问题。 在一个post请求中,有两个参数。 第一个是学号,第二个参数是"%B2%E9+%D1%AF%28Q%29" 这样的一个东西,好吧,这货经过gb2312转码之后 就是“查 询(Q)”这句话。真是坑爹啊,居然还用中文作为post请求的识别符。 请教一下怎么才能构建这种post请求呢?

var req = http.request(options, function (res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
})
buf='TXTXH=1105020215&CmdFind=查 询(Q)'
req.write(buf)
req.end();

这里面的buf怎么构建才正确呢?

###----------update------------ 结合各位大神的回复,给个例子记录一下吧 ####post请求中form数据gb2312编码(或者其他的非utf)

var urlencode = require('urlencode2');

var data = { //something } var buf = urlencode.stringify(data, {charset: ‘gb2312’});

var options = { hostname: ‘hostname’, port: 80, path: ‘/yourpath’, method: ‘POST or GET’, headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded’, ‘Content-Length’: buf.length //这个是万万不能没有啊,否则post就不正确了,具体原因不明 }

};

var req = http.request(options, function (res) { console.log('STATUS: ’ + res.statusCode); console.log('HEADERS: ’ + JSON.stringify(res.headers)); res.on(‘data’, function (chunk) { console.log('BODY: ’ + chunk); }); }) //错误处理 req.on(‘error’, function(e) { console.log('problem with request: ’ + e.message); }); req.write(buf) req.end();


4 回复

为了构建包含 GB2312 编码的 POST 请求,我们需要对表单数据进行适当的编码,并确保设置正确的 Content-Length 头。以下是一个完整的示例代码,展示了如何使用 Node.js 构建这样的请求。

首先,我们需要安装 urlencode2 模块来处理 GB2312 编码。你可以通过 npm 安装它:

npm install urlencode2

然后,你可以使用以下代码来构建和发送请求:

const http = require('http');
const urlencode = require('urlencode2');

// 设置请求选项
const options = {
    hostname: 'example.com', // 替换为实际的主机名
    port: 80,
    path: '/yourpath', // 替换为实际的路径
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
};

// 构建表单数据并进行 GB2312 编码
const data = {
    TXTXH: '1105020215',
    CmdFind: '查询(Q)' // 中文字符将自动被编码为 GB2312
};
const buf = urlencode.stringify(data, { charset: 'gb2312' });

// 设置 Content-Length 头
options.headers['Content-Length'] = Buffer.byteLength(buf);

// 创建请求
const req = http.request(options, (res) => {
    console.log(`STATUS: ${res.statusCode}`);
    console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
    let body = '';
    res.on('data', (chunk) => {
        body += chunk;
    });
    res.on('end', () => {
        console.log('BODY: ' + body);
    });
});

// 错误处理
req.on('error', (e) => {
    console.error(`Problem with request: ${e.message}`);
});

// 发送请求
req.write(buf);
req.end();

解释

  1. 引入依赖

    • http 模块用于创建 HTTP 请求。
    • urlencode2 模块用于处理 GB2312 编码。
  2. 设置请求选项

    • hostnameport 需要替换为实际的主机名和端口。
    • path 需要替换为实际的请求路径。
    • method 设置为 POST
    • headers 包含 Content-TypeContent-Length
  3. 构建表单数据

    • 使用 urlencode.stringify 方法将表单数据转换为 GB2312 编码的字符串。
  4. 设置 Content-Length 头

    • Content-Length 头需要设置为请求体的长度,以确保服务器能够正确解析请求体。
  5. 创建请求并发送

    • 使用 http.request 创建请求对象。
    • 监听请求的响应,并处理错误。
    • 使用 req.write 发送请求体。
    • 使用 req.end 结束请求。

通过这种方式,你可以成功地构建和发送包含 GB2312 编码的 POST 请求。


encodeURIComponent(“查 询(Q)”) === “%E6%9F%A5%20%E8%AF%A2(Q)”

图尔特特 5691.jpg

根据你的描述,你需要构建一个包含 GB2312 编码数据的 POST 请求。以下是完整的示例代码,并解释如何构建 buf

var urlencode = require('urlencode2');
var http = require('http');

var data = {
    TXTXH: '1105020215',
    CmdFind: '查询(Q)'
}

var buf = urlencode.stringify(data, {charset: 'gb2312'});

var options = {
    hostname: 'hostname',
    port: 80,
    path: '/yourpath',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(buf, 'binary')
    }
};

var req = http.request(options, function (res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

req.write(buf);
req.end();

解释:

  1. 引入依赖:首先引入 urlencode2http 模块。
  2. 定义数据:将需要发送的数据放在一个对象里。
  3. 编码数据:使用 urlencode.stringify 方法将数据字符串化,并指定编码为 gb2312
  4. 设置请求选项:配置请求的主机名、端口、路径、方法和头部信息。
  5. 创建请求:使用 http.request 创建请求实例。
  6. 监听错误:添加错误监听器处理可能的请求错误。
  7. 写入数据并结束请求:通过 req.write 将编码后的数据写入请求体,并调用 req.end 结束请求。

这样就可以正确地构建并发送包含 GB2312 编码数据的 POST 请求。

回到顶部