狀態碼302為啥
var http=require('http')
var querystring=require('querystring');
var contentData = {
? ? 'content':'努力學習,天天向上',
? ? 'cid':637
};
var pataData=querystring.stringify(contentData);
var options={
hostname:'www.immooc.com',
port:'80',
path:'/course/docomment',
method:'post',
headers:{
'Accept':'application/json, text/javascript, */*; q=0.01',
? ? ? ? 'Accept-Encoding':'gzip, deflate',
? ? ? ? 'Accept-Language':'zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4',
? ? ? ? 'Connection':'keep-alive',
? ? ? ? 'Content-Length':pataData.length,
? ? ? ? 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
? ? ? ? 'Cookie':'imooc_uuid=76e16dc8-6893-46f6-a732-8a287d536c3c;imooc_isnew_ct=1468987904;loginstate=1;apsid=kwNWQwYTI5ZGMyYjliMzdlNjBlMDNkYjdmNmYwZTIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMzU0MzU5OAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1MDE4OTQxODhAcXEuY29tAAAAAAAAAAAAAAAAAAAAADNmYzc4YzgyMzI3NjM3YTQ5YjE5NDYzNzk1ZGU0YTQ4UaOmV1Gjplc%3DMj;last_login_username=501894188%40qq.com;bdshare_firstime=1471354375168;PHPSESSID=h396rj39qrggq61tr37qfejkj1;imooc_isnew=2;cvde=57c6df1ab5074-26;IMCDNS=0;Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1471961948,1472012268,1472031648,1472651040;Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1472654818',
? ? ? ? 'Host':'www.xianlaiwan.cn',
? ? ? ? 'Origin':'http://www.xianlaiwan.cn',
? ? ? ? 'Referer':'http://www.xianlaiwan.cn/comment/637',
? ? ? ? 'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36',
? ? ? ? '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){
? ? ? conosle.log(Buffer.isBuffer(chunk))//判斷是否是buffer類型
? ? ? console.log(typeof chunk);
? ? ?})
? ? ?res.on('end',function(){
? ? ? console.log('評論成功');
? ? ?})
})
? ? ?req.on('error',function(e){
? ? ? ?console.log('Error: '+e.message)
? ? })
? ? req.write(pataData)
? ? req.end()
出現錯誤
Status: 302
headers: {"server":"Apache-Coyote/1.1","set-cookie":["JSESSIONID=9F1F6E3B879C6D0508131A1997D1E19D; Path=/; HttpOnly"],"location":"http://www.xianlaiwan.cn/login.do?custome_url=http://w
ww.imooc.com/course/docomment","content-length":"0","date":"Wed, 31 Aug 2016 16:00:55 GMT"}
評論成功
2016-09-05
兄弟,你hostname寫錯了
是www.xianlaiwan.cn
2016-09-15
兄弟,我和你出了一樣的問題,解決了能留言告訴下嗎
2016-09-01
現在又報出?
$ node request.js
Status: 403
headers: {"server":"Beaver","cache-control":"no-cache","content-type":"text/html","content-length":"594","connection":"close"}
true
object
評論成功
2016-09-01
臨時重定向,新的URL會在response 中的Location中返回,瀏覽器將會自動使用新的URL發出新的Request