0

我在执行发布请求时收到“套接字挂断”错误。我无法解决它。

  sparqlQ = getSPARQLPrefix() + query_string;
  console.log(sparqlQ)
  var options = {
    host: process.env['SESAME_HOST'],
    port: process.env['SESAME_PORT'],
    method: 'POST',
    path:
      '/openrdf-sesame/repositories/myReo?update=' +
      encodeURIComponent(sparqlQ) +
      '&content-type=application/sparql-results+json',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/sparql-results+json',
    },
  };

  var req = http.request(options, function(res) {
    var data = "";
    res.on('data', function (chunk) {
      data += chunk;
    });
    res.on('error', function (error) {
      console.log(error)
    });
    res.on('end', function () {
       console.log(data)
       req.end();
       callback(null);
    });
  }).on('error', function(e) {
    console.alert("Error getting sesame response [%s]", e.message);
    req.end();
    callback(e.message);
    return
  });

我究竟做错了什么?请帮忙!

4

1 回答 1

1

这里要提两件事。


你没有调用req.end()你的http request.

请参阅node.js 的 http 模块上的 此文档。

使用http.request()必须始终调用req.end()以表示 您已完成请求- 即使没有数据写入请求正文。


req.error你打电话的情况下console.alert,我认为应该是console.log


这是一个示例代码

http = require("http"); 
var options = {
    host: "localhost",
    port: 80,
    method: 'POST'      
};

var req = http.request(options, function(res) {

    var data = "";
    res.on('data', function (chunk) {
        data += chunk;
    });
    res.on('error', function (error) { });
    res.on('end', function () {
        console.log(data)
        req.end();
        console.log(null);
    });

}).on('error', function(e) {

        console.log("Error getting sesame response [%s]", e.message);
        req.end();
        console.log(e.message);
        return

});

req.end();
于 2013-08-19T04:50:01.770 回答