用nodejs 做客户端向别的服务器发送请求的时候 如果别的服务器的响应码是自定义的 而不是默认的,
比如说会出现20000的响应码 这种报错怎末解决
exports.get = function(req, res){
var data = '<Request>'+
' <Id>‘+’11’+’</Id>'+
'</Request>'
var opt = {
method: "POST",
host: interfaceConfig.emallServer,
port: interfaceConfig.emallPort,
path: interfaceConfig.emallUser,
headers: {
"Content-Type": 'text/xml’,
"Content-Length": data.length
}
};
var request = http.request(opt, function (serverFeedback) {
serverFeedback.setEncoding(‘utf8’);
serverFeedback.on('data’, function (data) {
console.log(data);
});
});
request.write(data + “\n”);
request.end();
};
报错:
Error :Parse Error
at Socket.socketOnData (http.js :1583:20)
at Tcp.onread(net.js:527:27)
自己抓包看了 响应码是20004但没有响应描述 报错如上 这种问题怎么解决啊
POST /emall/interface/getUserInfoByToken HTTP/1.1
Content-Type: text/xml
Content-Length: 48
Host: 211.140.7.142
Connection: keep-alive
<Request> <Id>11</Id></Request> HTTP/1.1 20004
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=C8DEB8E76971731937CCF9CCA9D01B39; Path=/emall/; HttpOnly
Content-Length: 0
Date: Thu, 15 May 2014 03:10:30 GMT
比较奇怪的设计. 好像nodejs的http没戏(不确定), 底层http parser报的错:
// deps/http_parser/http_parser.c
if (parser->status_code > 999) { // 如果自定义的状态码,超过了999, 直接报错 ==> 没有超过的话, 仍然是可接受的
SET_ERRNO(HPE_INVALID_STATUS);
goto error;
}
// lib/http.js
var ret = parser.execute(d, start, end - start);
if (ret instanceof Error) {
debug('parse error');
freeParser(parser, req);
socket.destroy();
req.emit('error', ret);
req._hadError = true;
}
照这样来看, 走 Socket 应该是行得通的, 只是很麻烦(麻烦到几乎自己要实现部分HTTP协议):
var net = require('net');
var client = net.connect({port: your_port, host:'your_host'},
function() { //'connect' listener
console.log('client connected');
client.write('GET / HTTP/1.1\r\n\r\n'); // 模拟 HTTP请求
});
client.on('data', function(data) {
console.log(data.toString()); // 响应来了, 还要解析 --- 工作量巨增
client.end();
});
client.on('end', function() {
console.log('client disconnected');
});
其它方法未知, 可能存在更容易的做法(修改nodejs源码, 再编译?).