跳到主要内容
CNode

不太理解https.get()怎么加入headers?

问答
Ssmarttang发布于 10 年前最后回复 10 年前
954160

我看了下api貌似没有讲。。。

const https = require('https');

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

https.get({host:'https://10.*.*.*:8183/',headers:{'user-agent':'fuckoude'}},(res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
  	console.log('..');
    // process.stdout.write(d);
  });

}).on('error', (e) => {
  console.error(e);
});
查看回复

回复 (9)

S
smarttang#910 年前
引用 wssgcg1213同步请求和异步请求的本质没区别呀,用 promise 就搞定了。。。。

@wssgcg1213 这个模块在centos 6.5 安装不太友好。。

W
wssgcg1213#810 年前

同步请求和异步请求的本质没区别呀,用 promise 就搞定了。。。。

S
smarttang#710 年前
引用 wssgcg1213https.request(options, callback)

@wssgcg1213 这个也是异步请求。。。 虽然自己已经实现了,但是还是特别感谢你回复。。。 我的整个笔记在这:http://www.jianshu.com/p/dd5d6b77f1e7

S
smarttang#610 年前
引用 i5ting@smarttang 这样,你用request模块

@i5ting 额。。这个是异步的,我要同步请求。。。。

W
wssgcg1213#510 年前

https.get 本身就是对 https.request 方法的一个包装

W
wssgcg1213#410 年前

https.request(options, callback)

const https = require('https');

var options = {
  hostname: 'encrypted.google.com',
  headers: {
    // 你要的header
  },
  port: 443,
  path: '/',
  method: 'GET'
};

var req = https.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});
req.end();

req.on('error', (e) => {
  console.error(e);
});
S
smarttang#210 年前
引用 i5tinghttps://github.com/i5ting/oh http

@i5ting 认真的扫了一下,感觉这个跟我的需求不搭啊。。。我是在client发起请求,不是做webserver。。。

参与回复

登录后即可参与回复。登录