如何通过HTTPS获取DBpedia内容?项目迁移后API访问受阻求助
Got it, let's walk through practical fixes for your situation—since browsers block mixed content (HTTPS pages loading HTTP resources), here are reliable ways to get around this:
后端中转(最推荐,适配绝大多数项目)
If your project has a backend service, let it handle the DBpedia HTTP API call, then pass the results back to your frontend via your own HTTPS endpoint. This way, your frontend only interacts with HTTPS resources, completely avoiding the mixed-content block.
Here's a quick Node.js (Express) example:const express = require('express'); const axios = require('axios'); const app = express(); app.get('/api/dbpedia-search', async (req, res) => { const query = req.query.QueryString; try { const dbpediaResponse = await axios.get( `http://lookup.dbpedia.org/api/search/KeywordSearch?QueryString=${encodeURIComponent(query)}` ); res.send(dbpediaResponse.data); } catch (err) { res.status(500).send('Failed to fetch data from DBpedia'); } }); app.listen(3000, () => console.log('Backend proxy running on port 3000'));Your frontend just needs to call your HTTPS backend's
/api/dbpedia-searchendpoint instead of directly hitting DBpedia's HTTP URL.搭建反向代理(适合无后端或需要独立代理的场景)
Use tools like Nginx or Cloudflare Workers to set up a reverse proxy that forwards HTTPS requests to DBpedia's HTTP API. Your frontend can then call the proxy's HTTPS address, and the proxy handles the HTTP call to DBpedia.
Example Nginx config snippet:server { listen 443 ssl; server_name your-proxy-domain.com; # Add your SSL certificate and key paths here ssl_certificate /path/to/your-cert.pem; ssl_certificate_key /path/to/your-key.pem; location /dbpedia-search { proxy_pass http://lookup.dbpedia.org/api/search/KeywordSearch; proxy_set_header Host lookup.dbpedia.org; proxy_set_header X-Real-IP $remote_addr; } }After setting this up, your frontend can request
https://your-proxy-domain.com/dbpedia-search?QueryString=your-search-termto get DBpedia data.Double-check DBpedia's HTTPS endpoint
Even though you mentioned HTTPS doesn't work, quickly verify if you're using the correct address. Try visitinghttps://lookup.dbpedia.org/api/search/KeywordSearch?QueryString=testdirectly in your browser. If it's a certificate trust issue, you could disable certificate validation in your backend requests (only do this temporarily, as it's not secure for long-term use).
Pro tip: Cache DBpedia's responses whenever possible—this reduces load on their servers and speeds up your project's performance.
内容的提问来源于stack exchange,提问作者Aakash Singh

