09 Node.js——HTTP-get/request

get是对request封装

可以在后台发起http请求,获取远程资源,更新或者同步远程资源

http.request(options[,callback])

 以下代码灌水失败:

var http = require('https')

var querystring = require('querystring')
//提交的表单数据Request Payload
var postData = querystring.stringify({
    "blogApp":"-beauTiFul",
    "body":"评论测试!!!",
    "parentCommentId":0,
    "postId":9102286,
})

var options = {
    //General
    hostname: 'www.cnblogs.com',
    port: 443,
    path: '/mvc/PostComment/Add.aspx',
    method: 'POST',
    //Request Headers
    headers:{
        //':authority':'www.cnblogs.com',
        //':method':'POST',
        //':path':'/mvc/PostComment/Add.aspx',
        //':scheme':'https',
        'accept':'application/json, text/javascript, */*; q=0.01',
        'accept-encoding':'gzip, deflate, br',
        'accept-language':'zh-CN,zh;q=0.8',
        'content-length':postData.length,
        'content-type':'application/json; charset=UTF-8',
        'cookie':'.CNBlogsCookie=6E153D1893CB37E731DB5EE8D21E661A7C049B5F3253BBF96697F61D3F80E228E35D96D67A0D9BCE96F2AC8BA8CEEBD3620CF8BF16272BA9F69993374453A28E325C7EBBD6F303CE4BE9A4AB4574AEC1F7659471; .Cnblogs.AspNetCore.Cookies=CfDJ8Gf3jjv4cttDnEy2UYRcGZ24spfp8lnfTvvwRTaSyrr3xil4hGj3F9ck-z2Jh14wCbuYEpPjN2je1GiahhBJmbD-DdW3GzYFsE8AG9O3BH9zjQClKJO72EMtqXW6Gf2wLLf_XjdRveKssOw_84c_DWFLC6pfSYzET8c4CKlkafKaq2Bhtd7f2o9KkA0vm5cCUqAQQ7YPrO1gspYwVIwnITYWBQ1yGnoQBSqjIXZek-Go6ipjFu4MHuIg-8uFogcSf6eWDSLe7v94cMmCEYdGF-4EccatEtxZbtcC7YdwRm4H; _ga=GA1.2.1898867304.1527349394; _gid=GA1.2.1085252201.1527349394; _gat=1',
        'origin':'https://www.cnblogs.com',
        'referer':'https://www.cnblogs.com/-beauTiFul/p/9102286.html',
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',
        'x-requested-with':'XMLHttpRequest'
    }
}

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(e){
        console.log('Error:' + e.message)
    })

    req.write(postData)

    req.end()

发布了241 篇原创文章 · 获赞 14 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_29150765/article/details/81300591
09