新手请教一个问题。。
发布于 11 年前 作者 ibigbug 4780 次浏览 最后一次编辑是 8 年前

有这样一个需求,就是模拟浏览器登录。 需要这样做:

  • 先get一次某url :LOGIN_URL拿到必须的参数
  • 接着post这个LOGIN_URL,把上一步get到的数据重新组织POST过去

我用的是needle

这样

needle.get(LOGIN_URL, function(err, resp, body){
    //process parameters
    needle.post(LOGIN_URL, function(err, resp, body){
        //到这里就会卡住,post不出去,
    })
})

请问该怎么解决?

12 回复

POST 不出去? 是报错了还是回调函数没有执行?

推荐用request : https://github.com/mikeal/request 个人觉得needle不太好用

回调执行了,post那一块卡住了,因为超时

谢谢,试一下

@ibigbug 超时为啥不怀疑是服务器问题?

@jiyinyiyong 因为换成别的URL一样超时

@jiyinyiyong 擦。好像还真是服务器的问题。不好意思我再测试一下。。刚来Node,有很多不知道的地方。。

@ibigbug GET请求只需要发送header即可,POST请求还需要发送body的,你这个代码很明显就没有发送body嘛,仔细看一下文档怎么用吧

@ibigbug 文档中的示例是这样的:

var data = {
  token: 'verysecret',
  payload: {
    value: JSON.stringify({ title: 'test', version: 1 }),
    content_type: 'application/json'
  }
}

needle.post('http://test.com/', data, { timeout: 5000, multipart: true }, function(err, resp, body){
  // in this case, if the request takes more than 5 seconds
  // the callback will return a [Socket closed] error
});

参考:https://github.com/tomas/needle

不知道你有没有看过文档!!!

还有,帖子标题应该简单地描述一下问题,而不是“新手请教一个问题。。”

@leizongmin 好吧。。确实确实没仔细看。。想当然了。。。

request的cookie你成功过没,无论怎么样cookiejar都是空的

sorry,因为本来cookie就是空的。。。

回到顶部