Home > Web Front-end > JS Tutorial > Detailed explanation of nodejs sending http request through proxy proxy

Detailed explanation of nodejs sending http request through proxy proxy

小云云
Release: 2018-02-03 11:32:58
Original
2283 people have browsed it

This article mainly introduces nodejs to send http requests through proxy. It has certain reference value. If you are interested, you can learn about it. I hope it can help everyone.

There may be such a demand, which requires node to be used as a web server to send http or https requests through another http/https proxy server. Without further ado, everyone knows the code directly:


var http = require('http')
var opt = {
 host:'这里放代理服务器的ip或者域名',
 port:'这里放代理服务器的端口号',
 method:'POST',//这里是发送的方法
 path:' https://www.google.com',   //这里是访问的路径
 headers:{
 //这里放期望发送出去的请求头
 }
}
//以下是接受数据的代码
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();
Copy after login

In this way, we send an https request through the designated proxy server. Note that we use the http protocol with the proxy server here, not https. Of course, the returned result will be based on your The proxy servers vary.


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>
Copy after login

Google returned a 302, telling us that to jump, we need to visit https://www.google.com.tw/ this address

Related recommendations:

Share several ways of sending HTTP requests in PHP

How to send HTTP requests in Java through HttpClient

Introduction to how php sends HTTP requests

The above is the detailed content of Detailed explanation of nodejs sending http request through proxy proxy. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template