https请求报错{ [Error: socket hang up] code: 'ECONNRESET', sslError: undefined }
发布于 11 年前 作者 marsbaiyun 21578 次浏览 最后一次编辑是 8 年前

在模拟一个post请求去抓取返回页面中的一些数据,该请求是https的,代码如下:

var https = require('https');

var url = "https://cargo.china-airlines.com/CCNet/pgFreightStatus/cgoTrack.aspx?AWBPrefix=297&AWBNumber=86097782";

var post_option = require('url').parse(url);
post_option.method = 'POST';
post_option.port = 443;
var post_data = require('querystring').stringify({
    ScriptManager1: 'ScriptManager1|btnQuery',
    __EVENTTARGET: '',
    __EVENTARGUMENT: '',
    __VIEWSTATE: '/wEPDwUKMTY2MDI5MzU5MQ9kFgICAw9kFggCAg8PFgQeCkZ1bmNfVGl0bGVlHhBGdW5jX0Rlc2NyaXB0aW9uZWQWCgIBDw8WAh4EVGV4dGVkZAIDDw8WAh8CZGRkAgYPDxYCHwJlFggeC29ubW91c2Vtb3ZlBVl2YXIgZHZIaW50PWRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCdkdkhpbnQnKTsgZHZIaW50LmlubmVySFRNTD0nJztTaG93SGludChldmVudCxkdkhpbnQpOx4Kb25tb3VzZW91dAWJAXZhciBkdkhpbnQ9ZG9jdW1lbnQuZ2V0RWxlbWVudEJ5SWQoJ2R2SGludCcpOyBkdkhpbnQuc3R5bGUuZGlzcGxheT0nbm9uZSc7IHRoaXMuY2xhc3NOYW1lPXRoaXMuY2xhc3NOYW1lLnJlcGxhY2UoLyBpbmZvdGl0bGVob3Zlci9nLCAnJyk7HgdvbmNsaWNrBWNqYXZhc2NyaXB0OlRpdGxlT25DbGljaygnJywnL1BYMDQwMjIvUmN2T25DYWxsRGF0YS5hc3B4P29uY2FsbF9mdW5jX3RpdGxlPSZub21lbnU9WSZub3RpdGxlbWVudT1ZJykeC29ubW91c2VvdmVyBSR0aGlzLmNsYXNzTmFtZSArPSAnIGluZm90aXRsZWhvdmVyJztkAgoPDxYCHwIFBy9DQ05ldC9kZAIMD2QWAmYPZBYEAgMPDxYCHwIFEDE1QXByIDAyOjM0OjI5eiBkZAIFDw8WAh4HVmlzaWJsZWhkZAIGDw9kFgIeB09uQ2xpY2sFO2phdmFzY3JpcHQ6IHJldHVybiBDaGVja0FXQigndHh0QVdCUHJlZml4JywndHh0QVdCTnVtYmVyJyk7ZAIHD2QWAmYPZBYKAgcPPCsADQEBEBYCAggCCRYCPCsABQEAFgIfB2c8KwAFAQAWAh8HZxYCZmZkAgsPPCsADQBkAg8PPCsADQEBEBYCAgYCBxYCPCsABQEAFgIfB2c8KwAFAQAWAh8HZxYCZmZkAhMPPCsADQEBEBYBAgkWATwrAAUBABYCHwdnFgFmZAIXDzwrAA0AZAIID2QWAmYPZBYCAgMPPCsADQBkGAcFCWd2RkFUQVdCQQ9nZAUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFCGJ0blF1ZXJ5BQxndkZBVkdIQUFfQU0PZ2QFD2d2RkFURU5WTERldGFpbA9nZAUFZ3ZBTVMPZ2QFCWd2RkFURlNVQQ9nZAUMZ3ZGQVRFTlZMTWF4D2dkngWKmvQSD9ztUEc3/x9UNOGHue0=',
    hdn_menu_head_index: '',
    p: 'SEARCH',
    o: '4',
    v: 'root',
    r: '2',
    currentCalForm: 'dep',
    currentdate: '114-04-15',
    onload: '',
    CCNetInfo1$txtCurrentDate: '',
    txtAWBPrefix: '297',
    txtAWBNumber: '86097782',
    __ASYNCPOST: 'true'
});
post_option.headers = {
    'Content-Type' : 'application/x-www-form-urlencoded',
    'Content-Length' : post_data.length
    };
var post_req = https.request(post_option, function(res){

    res.on('data', function(buffer){
        console.log(buffer.toString());
    });

});

post_req.on('error', function(err){
    console.log(err);
});

post_req.write(post_data);
post_req.end();

报错如下:{ [Error: socket hang up] code: 'ECONNRESET', sslError: undefined },添加代码post_option.secureOptions = require('constants').SSL_OP_NO_TLSv2;之后依然报这个错。之前做的http请求都没有问题,就是https怎么弄都不行啊,这是哪儿的问题啊?

6 回复

“Error Connection Reset” 我觉着像是请求被服务端关掉了? http://stackoverflow.com/questions/17245881/node-js-econnreset

你试一下用 request 请求一下?

request试过了,还是报同样的错误,代码如下:

var postData={
    ScriptManager1: 'ScriptManager1|btnQuery',
    __EVENTTARGET: '',
    __EVENTARGUMENT: '',
    __VIEWSTATE:     '...',//太长了,省略
    hdn_menu_head_index: '',
    p: 'SEARCH',
    o: '4',
    v: 'root',
    r: '2',
    currentCalForm: 'dep',
    currentdate: '114-04-15',
    onload: '',
    CCNetInfo1$txtCurrentDate: '',
    txtAWBPrefix: '297',
    txtAWBNumber: '86097782',
    __ASYNCPOST: 'true',
    'btnQuery.x': '-1045',
    'btnQuery.y': '-195'
};

require('request').post({
uri:"https://cargo.china-airlines.com/CCNet/pgFreightStatus/cgoTrack.aspx?   AWBPrefix=297&AWBNumber=86097782",
    headers:{
    'content-type': 'application/x-www-form-urlencoded',
    'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'
    },
    body:require('querystring').stringify(postData),
    secureOptions: require('constants').SSL_OP_NO_TLSv2
    },function(err,res,body){
    console.log(err);
    console.log(body);
    console.log(res.statusCode);
});

同样的请求我用javaHttpClient是能拿到返回的数据的,是我上面的代码有问题吗?

@marsbaiyun

试一下

request = request.defaults({
  strictSSL: false, // allow us to use our self-signed cert for testing
  rejectUnauthorized: false
});

@youxiachai 我现在怀疑是我访问的这个地址服务器的问题了,同样的代码: 获取google首页页面:

var request = require('request');
request('https://www.google.com.hk/', function (error, response, body) {
    console.log(error);
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
})

正常

获取我需要抓取数据的网站首页页面:

var request = require('request');
request('https://cargo.china-airlines.com/CCNet/pgHome/index.aspx/', function (error, response, body) {
    console.log(error);
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
})

返回{ [Error: socket hang up] code: 'ECONNRESET', sslError: undefined }

不过现在问题解决了,options加了个参数secureProtocol: 'SSLv3_method',代码如下:

var request = require('request');

var postData={
    ScriptManager1: 'ScriptManager1|btn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IfB2c8KwAFAQAWAh8HZxYCZmZkAhMPPCsADQEBEBYBAgkWATwrAAUBABYCHwdnFgFmZAIXDzwrAA0AZAIID2QWAmYPZBYCAgMPPCsADQBkGAcFCWd2RkFUQVdCQQ9nZAUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFCGJ0blF1ZXJ5BQxndkZBVkdIQUFfQU0PZ2QFD2d2RkFURU5WTERldGFpbA9nZAUFZ3ZBTVMPZ2QFCWd2RkFURlNVQQ9nZAUMZ3ZGQVRFTlZMTWF4D2dkngWKmvQSD9ztUEc3/x9UNOGHue0=',
    hdn_menu_head_index: '',
    p: 'SEARCH',
    o: '4',
    v: 'root',
    r: '2',
    currentCalForm: 'dep',
    currentdate: '2014-04-15',
    onload: '',
    CCNetInfo1$txtCurrentDate: '',
    txtAWBPrefix: '297',
    txtAWBNumber: '86097782',
    __ASYNCPOST: 'true',
    'btnQuery.x': '-1045',
    'btnQuery.y': '-195'
};

postData = require('querystring').stringify(postData);


var options = {
    uri: 'https://cargo.china-airlines.com/CCNet/pgFreightStatus/cgoTrack.aspx',
    body: postData,
    headers: {
        'Accept': '*/*',
        'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'Cookie': 'ASP.NET_SessionId=zikhfrrprfylac454hgu342u',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36',
        'Content-Length': postData.length,
        'X-MicrosoftAjax': 'Delta=true'
    },
    secureProtocol: 'SSLv3_method'
}
request.post(options, function (error, response, body) {
    console.log(error);
    console.log(response);
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
})

谢谢 @youxiachai 你了!

回到顶部