Electron中通过net的API发出HTTP请求
生活随笔
收集整理的這篇文章主要介紹了
Electron中通过net的API发出HTTP请求
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
場景
用HTML和CSS和JS構(gòu)建跨平臺桌面應用程序的開源庫Electron的介紹以及搭建HelloWorld:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/106413828
Electron怎樣進行渲染進程調(diào)試和使用瀏覽器和VSCode進行調(diào)試:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/106414541
在上面搭建好項目以及知道怎樣進行調(diào)試后,那么Electron怎樣實現(xiàn)發(fā)送HTTP請求。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費下載。
實現(xiàn)
首先在Index.html中新增一個按鈕,并設(shè)置id屬性
<button id="netRequest">請求網(wǎng)絡(luò)</button>然后在renderer.js中通過id獲取這個id并發(fā)動請求
var btnNetRequest=document.getElementById('netRequest'); btnNetRequest.onclick = NetRequest;function NetRequest() {const {net} = require('electron').remote;const request = net.request('https://blog.csdn.net/badao_liumang_qizhi');request.on('response', (response) => {console.log(`**statusCode:${response.statusCode}`);console.log(`**header:${JSON.stringify(response.headers)}`);response.on("data", (chunk)=>{console.log("接收到數(shù)據(jù):", chunk.toString());})response.on('end', () => {console.log("數(shù)據(jù)接收完成");})});//結(jié)束請求,不然沒有響應數(shù)據(jù)request.end(); }效果
?
?
總結(jié)
以上是生活随笔為你收集整理的Electron中通过net的API发出HTTP请求的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Electron中实现菜单、子菜单、以及
- 下一篇: Node的Web应用框架Express的