8138870
如题:
在nodejs一次请求中,需要一client方式多次和不同的host交互数据,我采用了http.request()遇到问题。第一次clienthttp请求可以响应回数据,接着拿数据做第二次请求,数据交互就出现影响了。
基本实现逻辑是:
在nodejs的routes上调用一个service,这个service拿到route传过来的请求参数,然后发起http.request()请求,在callback里判定数据,合法接着用http.request()响应的数据继续发起http.request()请求,这个时候的host已经不同,问题出来了。在sockets上有两个host值,第一次成功的请求host也在里面了,然后这次的数据交互就失败了。
我单独做不同host的http.request()数据交互是正确的。
下面是console.log()信息,有没有办法在第二次发起http.request()的时候,没有第一次host缓存,不能单独做两次nodejs的请求来分离这个逻辑。
{ host: '192.168.1.249',
port: 80,
path: '/yy.action',
method: 'POST',
headers: { 'Content-Type': 'text/xml; charset=UTF-8' } }
{ output: [],
outputEncodings: [],
writable: true,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: true,
_hasBody: true,
_trailer: '',
finished: false,
agent:
{ options: {},
requests: {},
sockets:
{ '192.168.1.105:8080': [Object],
'192.168.1.249:80': [Object] },
maxSockets: 5,
_events: { free: [Function] },
createConnection: [Function] },
socketPath: undefined,
method: 'POST',
path: '/yy.action',
_events:
{ response: { [Function: g] listener: [Function] },
socket: { [Function: g] listener: [Function] } },
_headers:
{ 'content-type': 'text/xml; charset=UTF-8',
host: '192.168.1.249' },
_headerNames: { 'content-type': 'Content-Type', host: 'Host' } }