node.js http客户端
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                node.js http客户端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                ? ? ?一、http模塊提供了兩個函數http.request和http.get,功能是作為客戶端向HTTP服務器發起請求。
Ext.Ajax.request({},function(response))????????????1.http.request(options,callback)發起HTTP請求,接受兩個參數,option是一個類似關聯數組的對象,表示請求的參數,callback是請求的回調函數,option常用的參數如下
host:請求網站的域名或IP地址 port:請求網站的端口,默認是80, method:請求方法,模式是GET/POST path:請求的相對于根的路徑,默認是"/"。QueryString應該包含在其中,例如/search?query=marico headers:一個關聯數組對象,為請求頭的內容 callback傳遞一個參數,為http.ClientResponse的實例 http.request返回一個http.ClientRequest的實例 案例:clientRequest.js var http=require('http'); var querystring=require('querystring'); //啟動服務 http.createServer(function(req,res){console.log('請求到來,解析參數');//解析post請求var post='';req.on('data',function(chunk){post+=chunk;});req.on('end',function(){post=querystring.parse(post);//解析完成console.log('參數解析完成,返回name參數');res.end(post.name);}); }).listen(3000);//客戶端請求 var contents=querystring.stringify({name:'marico',age:21,address:'beijing' }); //Ext.encode(); //聲明請求參數 var options={host:'localhost',path:'/',port:3000,method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded','Content-Length':contents.length} }; //發送請求 var req=http.request(options,function(res){res.setEncoding('utf-8');res.on('data',function(data){console.log('后臺返回數據');console.log(data);}) }); req.write(contents); //必須調用end() req.end();????????????2.http.get(options,callback) http模塊還提供了一個更加簡便的方法用于處理GET請求:http.get。它是http.request的簡化版,唯一的區別在于http.get自動將請求方法設為GET請求,同時不需要手動調用req.end();
案例:clientGet.js var http=require('http'); var url=require('url'); var util=require('util'); //啟動服務 http.createServer(function(req,res){console.log('請求到來,解析參數');var params=url.parse(req.url,true);console.log('解析完成');console.log(util.inspect(params));console.log('向客戶端返回');res.end(params.query.name); }).listen(3000);//客戶端請求 var request=http.get({host:'localhost',path:'/user?name=marico&age=21',port:3000},function(res){res.setEncoding('utf-8');res.on('data',function(data){console.log(' 服務端相應回來的數據為:'+data);}) });????二、http.ClientRequest
該對象是由http.request或http.get返回產生的對象,表示一個已經產生而且正在進行的HTTP請求,它提供了response事件,即http。request或http.get第二個參數制定的回調函數的綁定對象,請求必須調用end方法結束請求。 提供的函數: request.abort() 終止正在發送的請求 request.setTimeout(timeout,[callback]) 設置請求超時時間,timeout為毫秒數,當請求超時后,callback將會被調用 其它:request.setNoDelay([noDelay])、request.setScoketKeepAlive([enable],[initialDelay])等函數。 API地址:http://nodejs.org/api/http.html????三、http.ClientResponse
http.ClientReponse是與http.ServerResponse相似,提供三個事件,data、end和close,分別在數據到達,傳輸結束和連接結束時觸發,其中data事件傳遞一個參數chunk,表示接受到的數據 屬性,表示請求的結果狀態 statusCode?? HTTP狀態碼,如200,404,500 httpVersion:HTTP協議版本 headers:HTTP請求頭 trailers:HTTP請求尾 函數: response.setEncoding([encoding]):設置默認的編碼,當data事件被觸發時,數據將以encoding編碼。默認值為null,以buffer的形式存儲。 response.pause():暫停接受數據和發送事件,方便實現下載功能。 response.resume():以暫停的狀態中恢復總結
以上是生活随笔為你收集整理的node.js http客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Set实现类性能对比
 - 下一篇: 集合类三种遍历方式