WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 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

QuickApp 快应用中 或 nodejs 中 API接品调用时 GBK转UTF8

Posted on   WebEnh  阅读(290)  评论(0编辑  收藏  举报

请求接口地址:https://doc.quickapp.cn/features/system/fetch.html?h=fetch

第一步,安装包:

npm install iconv-lite
复制代码
  async onInit() {
    var prompt = require('@system.prompt');
    {
      // 这是转换前的GBK
      var resultFetchOld = await fetch.fetch({
        url: 'http://ip.ws.126.net/ipquery',
        responseType: 'text',
        method: 'GET',
        headers: {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'cache-control': 'no-cache',
        'pragma': 'no-cache',},
      });
      console.log('显示转换前的结果: ', resultFetchOld.data.data, '4秒后显示转换后的结果');
      // 显示转换前的结果
      setTimeout(function () {
        var htmlStr = '显示转换前的结果: ' + resultFetchOld.data.data + ',4秒后显示转换后的结果'
        prompt.showToast({ message: htmlStr });
      }, 0);
    }

    var resultFetch = await fetch.fetch({
      url: 'http://ip.ws.126.net/ipquery',
      responseType: 'arraybuffer',
      // responseType: 'text',
      method: 'GET',
      headers: {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'cache-control': 'no-cache',
        'pragma': 'no-cache',
      },
    });

    // console.log('resultFetch', resultFetch);
    // console.log('resultFetch.data.data', resultFetch.data.data)
    // console.log('resultFetch.data.data-stringify', JSON.stringify(resultFetch.data.data));

    //这里是引入包
    const Buffer = require('buffer').Buffer; // 这个包为 nodejs 自带,无需安装
    const iconvLite = require('iconv-lite');

    // let b1 = new Uint8Array(ipHtmlResult.data.data);
    //Buffer.from(b1,'hex')是把Uint8Array转化成Buffer类型数据
    let htmlStr = iconvLite.decode(Buffer.from(resultFetch.data.data, 'hex'), 'gbk');

    console.log('htmlStr', htmlStr);

    // 显示转换后的结果
    setTimeout(function () {
      htmlStr = '显示转换后的结果: ' + htmlStr
      prompt.showToast({ message: htmlStr });
    }, 4000);
  }
复制代码

如果未开启 async 支持,请参考 https://doc.quickapp.cn/tutorial/framework/using-async.html?h=async

 

 

 

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2016-11-02 《AngularJS高级程序设计》学习笔记
2016-11-02 原 ng-include用法分析以及多标签页面的简单实现方式
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多