这才是今天让我大开眼界的地方

操作呢也有一点点复杂。还是以慕课网为例吧。。其实哪个网站都可以,只要有评论功能都可以的吧。

http://www.imooc.com/comment/348 进到这里,注册个账号再操作哈,否则可能会被拦截,另外成功后不要只顾爽,万一被封号。。

 评论之前先打开 开发者工具--NetWork--发表评论---会多出来一个docomment(哇真的是这个名字害惨我了)--Request headers复制出来,随便找个地方先放着,页面不要关。

新建文件

var http = require('http')
var querystring = require('querystring')

var postData = querystring.stringify({
    'content':'测试一下2',
    'cid':348
})
var options={
    hostname:'www.imooc.com',
    port:80,
    path:'/course/docomment',//一定要认真复制过来
    method:'POST',
    headers:{
        //复制过来的,修改:键值对,要加上单引号
        //content-length:postData.length
    }
}



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()

  headers里边是刚刚复制下来的代码,但要做一些改动,添加单引号和逗号:

 'Accept':'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding':'gzip, deflate',
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Connection':'keep-alive',
        'Content-Length':postData.length,//不要加引号
        'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie':'UM_distinctid=15beabcd1b7131-0149534ca2496d-396b4e08-100200-15beabcd1b91b2; imooc_uuid=72ef5436-b5ca-4fad-aa51-a6a68657e678; imooc_isnew_ct=1494291895; wf_tid=b434831d8868; wf_rid=0cc03c1a020f; CNZZDATA1261110065=398582083-1494286575-https%253A%252F%252Fwww.baidu.com%252F%7C1499496727; loginstate=1; apsid=QxMjhhMWQ4MjhiMzFkMDI3ZGI4MjgwZjg0OTVmZjYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMzAwMjIyOQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGY3YWRjMjA0N2QxZDcxMzI3NDE3ZDc0ZWJkN2ZmZWVhnG9lWZxvZVk%3DMW; PHPSESSID=uviejkunukoenifojgf4jad9i4; IMCDNS=0; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1499875108,1499920320,1499924161,1499943591; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1499947257; imooc_isnew=2; cvde=596752a7d1e51-30',
        'Host':'www.imooc.com',
        'Origin':'http://www.imooc.com',
        'Referer':'http://www.imooc.com/comment/348',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
        'X-Requested-With':'XMLHttpRequest'
        //复制过来的,修改:键值对,要加上单引号
        //content-length:postData.length

  耗了不少时间,出现过status:302  和 404,也是找了好久的错误。

302就是postData那个地方加了单引号。

404就是docomment写成了document。。。长的很像的好不好。。。。

 

 

 

 

 status 是200才可以成功的。

 

 哇 我可以做水军了么O(∩_∩)O

posted on 2017-07-14 00:02  Amy鱼  阅读(602)  评论(0编辑  收藏  举报