選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

HTTPS

焦點模式
HTTPS - AWS IoT Core

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

用戶端可以使用 1.0 或 1.1 HTTP 通訊協定向 提出請求RESTAPI,以發佈訊息。如需 HTTP請求使用的身分驗證和連接埠映射,請參閱 通訊協定、連接埠映射和身分驗證

注意

HTTPS 不支援MQTT像 那樣clientId的值。 在使用 時clientId可以使用MQTT,但在使用 時無法使用HTTPS。

HTTPS 訊息 URL

裝置和用戶端透過向用戶端特定的端點和主題特定的 提出POST請求來發佈其訊息URL:

https://IoT_data_endpoint/topics/url_encoded_topic_name?qos=1
  • IoT_data_endpointAWS IoT 裝置資料端點。您可以使用 命令,在物件的詳細資訊頁面或用戶端 AWS CLI 的 AWS IoT 主控台中找到端點:

    aws iot describe-endpoint --endpoint-type iot:Data-ATS

    此端點看起來如下:a3qjEXAMPLEffp-ats.iot.us-west-2.amazonaws.com

  • url_encoded_topic_name 是所傳送訊息的完整主題名稱

HTTPS 訊息程式碼範例

這些是如何傳送訊息的範例HTTPS AWS IoT。

Python (port 8443)
import requests import argparse # define command-line parameters parser = argparse.ArgumentParser(description="Send messages through an HTTPS connection.") parser.add_argument('--endpoint', required=True, help="Your AWS IoT data custom endpoint, not including a port. " + "Ex: \"abcdEXAMPLExyz-ats.iot.us-east-1.amazonaws.com\"") parser.add_argument('--cert', required=True, help="File path to your client certificate, in PEM format.") parser.add_argument('--key', required=True, help="File path to your private key, in PEM format.") parser.add_argument('--topic', required=True, default="test/topic", help="Topic to publish messages to.") parser.add_argument('--message', default="Hello World!", help="Message to publish. " + "Specify empty string to publish nothing.") # parse and load command-line parameter values args = parser.parse_args() # create and format values for HTTPS request publish_url = 'https://' + args.endpoint + ':8443/topics/' + args.topic + '?qos=1' publish_msg = args.message.encode('utf-8') # make request publish = requests.request('POST', publish_url, data=publish_msg, cert=[args.cert, args.key]) # print results print("Response status: ", str(publish.status_code)) if publish.status_code == 200: print("Response body:", publish.text)
Python (port 443)
import requests import http.client import json import ssl ssl_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT) ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2 # note the use of ALPN ssl_context.set_alpn_protocols(["x-amzn-http-ca"]) ssl_context.load_verify_locations(cafile="./<root_certificate>") # update the certificate and the AWS endpoint ssl_context.load_cert_chain("./<certificate_in_PEM_Format>", "<private_key_in_PEM_format>") connection = http.client.HTTPSConnection('<the ats IoT endpoint>', 443, context=ssl_context) message = {'data': 'Hello, I'm using TLS Client authentication!'} json_data = json.dumps(message) connection.request('POST', '/topics/device%2Fmessage?qos=1', json_data) # make request response = connection.getresponse() # print results print(response.read().decode())
CURL

您可以使用 curl 從用戶端或裝置傳送訊息至 AWS IoT。

使用 curl 從 AWS IoT 用戶端裝置傳送訊息
  1. 檢查 curl 版本。

    1. 在您的用戶端上,請在命令提示中執行此命令。

      curl --help

      在說明文字中,尋找TLS選項。您應該看到 --tlsv1.2 選項。

    2. 如果您看到 --tlsv1.2 選項,請繼續。

    3. 如果您沒有看到 --tlsv1.2 選項或您收到 command not found 錯誤訊息,您可能需要在用戶端上更新或安裝 curl 或安裝 openssl,然後才能繼續進行。

  2. 在用戶端上安裝憑證。

    複製您在 AWS IoT 主控台中註冊用戶端 (物件) 時建立的憑證檔案。在繼續之前,請確定您的用戶端上有這三個憑證檔案。

    • CA 憑證檔案 (此範例中的 Amazon-root-CA-1.pem)。

    • 用戶端的憑證檔案 (此範例中的 device.pem.crt)。

    • 用戶端的私密金鑰檔案 (此範例中的 private.pem.key)。

  3. 建立 curl 命令列,取代您帳戶和系統的可取代值。

    curl --tlsv1.2 \ --cacert Amazon-root-CA-1.pem \ --cert device.pem.crt \ --key private.pem.key \ --request POST \ --data "{ \"message\": \"Hello, world\" }" \ "https://IoT_data_endpoint:8443/topics/topic?qos=1"
    --tlsv1.2

    使用 TLS 1.2 (SSL)。

    --cacert Amazon-root-CA-1.pem

    驗證對等的憑證授權機構憑證檔案名稱和路徑 (如有必要)。

    --cert device.pem.crt

    用戶端的憑證檔案名稱和路徑 (如有必要)。

    --key private.pem.key

    用戶端的私密金鑰檔案名稱和路徑 (如有必要)。

    --request POST

    HTTP 請求的類型 (在此情況下為 POST)。

    --data "{ \"message\": \"Hello, world\" }"

    您要發佈HTTPPOST的資料。在這種情況下,它是JSON字串,內部引號會逸出並帶有反斜線字元 (\)。

    "https://IoT_data_endpoint:8443/topics/topic?qos=1"

    用戶端 AWS IoT 的裝置資料端點URL的 ,後面接著HTTPS連接埠 :8443,接著關鍵字,/topics/以及主題名稱 topic,在這種情況下。指定服務品質作為查詢參數 ?qos=1

  4. 在 AWS IoT 主控台中開啟MQTT測試用戶端。

    遵循 中的指示使用 MQTT 用戶端檢視 AWS IoT MQTT 訊息,並設定主控台以訂閱 curl命令topic中使用的主題名稱為 的訊息,或使用 的萬用字元主題篩選條件#

  5. 測試命令。

    監控 AWS IoT 主控台測試用戶端中的主題時,請前往您的用戶端並發出您在步驟 3 中建立的 curl 命令列。您應該會在主控台中看到用戶端的訊息。

import requests import argparse # define command-line parameters parser = argparse.ArgumentParser(description="Send messages through an HTTPS connection.") parser.add_argument('--endpoint', required=True, help="Your AWS IoT data custom endpoint, not including a port. " + "Ex: \"abcdEXAMPLExyz-ats.iot.us-east-1.amazonaws.com\"") parser.add_argument('--cert', required=True, help="File path to your client certificate, in PEM format.") parser.add_argument('--key', required=True, help="File path to your private key, in PEM format.") parser.add_argument('--topic', required=True, default="test/topic", help="Topic to publish messages to.") parser.add_argument('--message', default="Hello World!", help="Message to publish. " + "Specify empty string to publish nothing.") # parse and load command-line parameter values args = parser.parse_args() # create and format values for HTTPS request publish_url = 'https://' + args.endpoint + ':8443/topics/' + args.topic + '?qos=1' publish_msg = args.message.encode('utf-8') # make request publish = requests.request('POST', publish_url, data=publish_msg, cert=[args.cert, args.key]) # print results print("Response status: ", str(publish.status_code)) if publish.status_code == 200: print("Response body:", publish.text)

在本頁面

下一個主題:

MQTT 主題

上一個主題:

MQTT
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。