为了账号安全,请及时绑定邮箱和手机立即绑定

同样使凤凰网 get请求 请求报404

var http=require('http')
var querystring=require('querystring')
var postData=querystring.stringify({
    'callback': 'postCmt',
    'docUrl': 'http://news.ifeng.com/a/20181102/60141155_0.shtml',
    'docName': '习近平:组织动员妇女走在时代前列建功立业',
    'speUrl':'',
    'skey': '3aba0b',
    'format': 'js',
    'content': '支持中华民族伟大复兴',
    'callback': 'postCmt',
    'permalink': 'http://news.ifeng.com/a/20181102/60141155_0.shtml'
})

var options={
    hostname:'comment.ifeng.com',
    port:80,//http默认端口80,https默认端口443
    path:'post.php?callback=postCmt&docUrl=http%3A%2F%2Fnews.ifeng.com%2Fa%2F20181102%2F60141155_0.shtml&docName=%E4%B9%A0%E8%BF%91%E5%B9%B3%EF%BC%9A%E7%BB%84%E7%BB%87%E5%8A%A8%E5%91%98%E5%A6%87%E5%A5%B3%E8%B5%B0%E5%9C%A8%E6%97%B6%E4%BB%A3%E5%89%8D%E5%88%97%E5%BB%BA%E5%8A%9F%E7%AB%8B%E4%B8%9A&speUrl=&skey=3aba0b&format=js&content=%E5%BE%88%E5%A5%BD%EF%BC%8C%E6%B0%B8%E8%BF%9C%E6%94%AF%E6%8C%81&callback=postCmt&permalink=http%3A%2F%2Fnews.ifeng.com%2Fa%2F20181102%2F60141155_0.shtml',
    method:'GET',
    headers:{
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Cookie': 'prov=cn021; city=021; weather_city=sh; region_ip=58.39.75.76; region_ver=1.30; userid=1541172871384_cc93wy1801; sid=5348E1CB5A971FC56C49AAC0253D7D54; IF_TIME=1541172919489871; IF_USER=%E5%BC%98%E6%89%AC0122; IF_REAL=0',
        'Host': 'comment.ifeng.com',
        'Referer': 'http://news.ifeng.com/a/20181102/60141155_0.shtml',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'

    }
}
var req=http.request(options,function (res) {
    console.log('Status:'+res.statusCode)
    console.log('headers:'+JSON.stringify(res.headers))
    res.on('data',function (chunk) {
        console.log(Buffer.isBuffer(chunk))
        console.log(typeof chunk)
    })
    res.on('end',function () {
        console.log('评论完毕')
    })
})
req.on('error',function () {
    console.log('获取错误')
})
req.write(postData)
req.end()



Status:400

headers:{"server":"nginx/1.10.2","content-type":"text/html","content-length":"173","accept-ranges":"bytes","date":"Fri, 02 Nov 2018 16:07:41 GMT","age":"0","x-hits":"0","x-cache":"MISS Cache","via":"iFeng Cache Server","content-security-policy":"upgrade-insecure-requests"}

true

object

评论完毕


正在回答

1 回答

我用你代码改了一下。

var options={

    hostname:'comment.ifeng.com',

    port:80,//http默认端口80,https默认端口443

    path:'/post.php',

    method:'GET',

    headers:{

        'Accept': '*/*',

        'Accept-Encoding': 'gzip, deflate',

        'Accept-Language': 'zh-CN,zh;q=0.9',

        'Connection': 'keep-alive',

        'Cookie': 'prov=cn021; city=021; weather_city=sh; region_ip=58.39.75.76; region_ver=1.30; userid=1541172871384_cc93wy1801; sid=5348E1CB5A971FC56C49AAC0253D7D54; IF_TIME=1541172919489871; IF_USER=%E5%BC%98%E6%89%AC0122; IF_REAL=0',

        'Host': 'comment.ifeng.com',

        'Referer': 'http://news.ifeng.com/a/20181102/60141155_0.shtml',

        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'


    }

}

改了path就成功了。
你先理解下url的组成。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
进击Node.js基础(一)
  • 参与学习       219296    人
  • 解答问题       982    个

本视频教程带你揭开Node.js的面纱,带你走进一个全新世界

进入课程

同样使凤凰网 get请求 请求报404

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号