nodejs 下post代码

时间: 2011-10-18 / 分类: 未分类 / 浏览次数: 32 views / 2个评论 发表评论
	var options = {
		host: 'www.yourdomain.com',
		post: 80,
		method: 'POST',
		path: '/',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded',
			'Content-Length': params.length
		}
	};

	var request = http.request(options, function(response) {
		response.setEncoding('utf8');
		response.on('data', function(chunk) {
			console.log(chunk);
		});
	});

 request.write(params);
 request.end();

2个评论

  1. admin
    2011/12/18 21:54:16

    看来你没有理解:’Content-Type’: ‘application/x-www-form-urlencoded’,
    这句的含义。
    urlencoded后的字符,是不可能存在中文字符的,全部是ascii码的字符。
    所以params.length不会有错误的。

  2. 熊佳斌
    2011/12/17 17:42:36

    强烈建议别写 ‘Content-Length’: params.length这句,如果params中包含中文,这个length是错误的,会导致发送的报文不完整。

发表评论

您的昵称 *

您的邮箱 *

您的网站