http
get请求
const http = require('http');
const httpOptions = {
// 这里一定是hostname
hostname: 'www.gov.cn',port: '80',method: 'get',path: '/',headers:{
useragent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/100.0.4896.60 Safari/537.36'
},}
let httpBody = '';
const httpReq = http.request(httpOptions,(res) =>{
res.on('data',(d)=> {
httpBody += d;
})
res.on('end',()=> {
console.log(`Request by http,response data: ${httpBody}`);
})
})
// 一定要调用end方法,否则请求一直pending,最后会让你怀疑人生
httpReq.end();
使用代理发送http请求
注意:以下方式未验证通过,但是可以通过http创建一个server,在server中解析请求,然后再通过request的方法发送实际的请求,这种方式一定可以,待续
const http = require('http');
const httpProxyOptions = {
// host 一定要配置为代理服务器的地址
host: '101.66.88.1',// port 一定要配置为代理服务器的端口
port: '6666',// 不再需要配置hostname
// hostname: 'www.baidu.com',// path 为实际要请求的地址
path: 'http://www.gov.cn',}
let httpProxyBody = '';
const httpProxyReq = http.request(httpProxyOptions,(d)=> {
httpProxyBody += d;
})
res.on('end',response data: ${httpProxyBody}`);
})
})
httpProxyReq.end();
https
get请求
const http = require('http');
const httpsOptions = {
hostname: 'www.baidu.com',port: '443',}
let httpsBody = '';
const httpsReq = https.request(httpsOptions,(d)=> {
httpsBody += d;
})
res.on('end',response data: ${httpsBody}`);
})
})
httpsReq.end();
原文地址:https://blog.csdn.net/thlzjfefe" target="_blank" rel="noopener" title="thlzjfefe">thlzjfefe</a> <img class="article-time-img article-heard-img" src="https://csdnimg.cn/release/blo
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。