使用 C++ 从 HTTPS 请求 JSON 数据?
Request JSON Data from HTTPS with C++?
我正在用 C++ 编写一个程序,需要从 HTTPS URL 下载 JSON 数据。该程序基于 wxWidgets。 URL 用于 Glosbe
的翻译服务
所以我尝试了多个不同的库,包括:
- libcurl
- Boost.Asio
- wxWidgets 中包含的 http 功能
- wxCurl
- 网址
然而,它总是抛出一个错误,说它无法连接,或者我收到一个回复说 "Moved Permanently"。
当我将 URL 复制并粘贴到浏览器中进行测试时,它 returns 完美地 JSON 数据。
有谁知道正确的做法吗?
任何帮助都会很棒!
301 Moved Permanently 是当您尝试使用 HTTP 而不是 HTTPS 访问页面时服务器的响应。这是我刚刚从服务器收到的完整响应:
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 16 Jul 2015 20:25:01 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://en.glosbe.com/a-api
确切的意思是:"The content you are looking for is really at https://en.glosbe.com/a-api." 您的浏览器只是遵循服务器的提示并自动遵守 HTTP 协议当您尝试访问 http://en.glosbe.com/a-api
时继续请求 https://en.glosbe.com/a-api
。作为用户,它可以无缝地为您服务。
您将需要阅读更多文档才能自己创建 HTTPS 请求。您提到的每个库都将以不同的方式支持 HTTPS(或根本不支持)。例如,查看 http://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/overview/ssl.html,尤其是 "Notes" 部分,其中显示 "OpenSSL is required to make use of Boost.Asio's SSL support."
我正在用 C++ 编写一个程序,需要从 HTTPS URL 下载 JSON 数据。该程序基于 wxWidgets。 URL 用于 Glosbe
的翻译服务所以我尝试了多个不同的库,包括:
- libcurl
- Boost.Asio
- wxWidgets 中包含的 http 功能
- wxCurl
- 网址
然而,它总是抛出一个错误,说它无法连接,或者我收到一个回复说 "Moved Permanently"。
当我将 URL 复制并粘贴到浏览器中进行测试时,它 returns 完美地 JSON 数据。
有谁知道正确的做法吗?
任何帮助都会很棒!
301 Moved Permanently 是当您尝试使用 HTTP 而不是 HTTPS 访问页面时服务器的响应。这是我刚刚从服务器收到的完整响应:
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 16 Jul 2015 20:25:01 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://en.glosbe.com/a-api
确切的意思是:"The content you are looking for is really at https://en.glosbe.com/a-api." 您的浏览器只是遵循服务器的提示并自动遵守 HTTP 协议当您尝试访问 http://en.glosbe.com/a-api
时继续请求 https://en.glosbe.com/a-api
。作为用户,它可以无缝地为您服务。
您将需要阅读更多文档才能自己创建 HTTPS 请求。您提到的每个库都将以不同的方式支持 HTTPS(或根本不支持)。例如,查看 http://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/overview/ssl.html,尤其是 "Notes" 部分,其中显示 "OpenSSL is required to make use of Boost.Asio's SSL support."