有可能有這樣的需求,需要node作為web服務(wù)器通過另外一臺http/https代理服務(wù)器發(fā)http或者h(yuǎn)ttps請求,廢話不多說直接上代碼大家都懂的:
var http = require('http')var opt = { host:'這里放代理服務(wù)器的ip或者域名', port:'這里放代理服務(wù)器的端口號', method:'POST',//這里是發(fā)送的方法 path:' https://www.google.com', //這里是訪問的路徑 headers:{ //這里放期望發(fā)送出去的請求頭 }}//以下是接受數(shù)據(jù)的代碼var body = '';var req = http.request(opt, function(res) { console.log("Got response: " + res.statusCode); res.on('data',function(d){ body += d; }).on('end', function(){ console.log(res.headers) console.log(body) });}).on('error', function(e) { console.log("Got error: " + e.message);})req.end();這樣我們就通過了指定代理服務(wù)器發(fā)出了https的請求,注意這里我們同代理服務(wù)器是http協(xié)議的,不是https,返回的結(jié)果當(dāng)然肯定會根據(jù)你的代理服務(wù)器不同有所不同。
Got response: 302{ location: 'https://www.google.com.tw/', 'cache-control': 'private', 'content-type': 'text/html; charset=UTF-8', 'set-cookie': [ 'PREF=ID=b3cfcb24798a7a07:FF=0:TM=1356078097:LM=1356078097:S=v_3qEd0_gCW6-xum; expires=Sun, 21-Dec-2014 08:21:37 GMT; path=/; domain=.google.com', 'NID=67=qoJf_z3W7KlibpNZ6xld__r0rYGyYu7l_XiDQmZ3anjBFadDzhijME3QcX651yucne_irK_2JMS8HF5FuxNl85mE0nDrtn9Iq0z2gW69n00OrB970hpHTbYe0mAogZit; expires=Sat, 22-Jun-2013 08:21:37 GMT; path=/; domain=.google.com; HttpOnly' ], p3p: 'CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."', date: 'Fri, 21 Dec 2012 08:21:37 GMT', server: 'gws', 'content-length': '223', 'x-xss-protection': '1; mode=block', 'x-frame-options': 'SAMEORIGIN', via: '1.0 ***.****.com:80 (squid/2.6.STABLE21)', 'proxy-connection': 'keep-alive' }<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"><TITLE>302 Moved</TITLE></HEAD><BODY><H1>302 Moved</H1>The document has moved<A href="https://www.google.com.tw/" rel="external nofollow" >here</A>.</BODY></HTML>谷歌返回了一個302,告訴我們進(jìn)行跳轉(zhuǎn),需要訪問 https://www.google.com.tw/ 這個地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點
疑難解答