bug_x

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

本文源于实践及其部分网络搜索:

其实大部分,官方都有介绍...

官方参考链接:https://nodejs.org/api/http.html
var http = require('http');
var querystring = require('querystring');
var options = {
        host: '127.0.0.1'// 请求地址 域名,google.com等..
        port:80,
        path:path, // 具体路径eg:/upload
        method: 'GET'// 请求方式, 这里以post为例
        headers: { // 必选信息,  可以抓包工看一下
            'Content-Type''application/json'
        }
    };
    http.get(options, function(res) {
        var resData = "";
        res.on("data",function(data){
            resData += data;
        });
        res.on("end"function() {
            callback(null,JSON.parse(resData));
        });
    })
(2):post 请求:
var postData = querystring.stringify({
  'msg' 'Hello World!'
});
 
var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type''application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  }
};
 
var req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.');
  });
});
 
req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});
 
// write data to request body
req.write(postData);
req.end();

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log("body: " + chunk);
    });
    res.on('end',function(chunk){
        console.log("body: " + chunk);
    })
});
参考链接:https://nodejs.org/api/http.html
/m1=ff&op=get
posted on   bug_x  阅读(9481)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
 
点击右上角即可分享
微信分享提示