本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
HTTPS
用戶端可以使用 HTTP 1.0 或 1.1 通訊協定向 REST API 發出請求來發佈訊息。如需 HTTP 請求所使用的身分驗證和連接埠對應的相關資訊,請參閱 通訊協定、連接埠映射和身分驗證。
注意
HTTPS 不像 MQTT 一樣可支援 clientId
值。使用 MQTT 時可使用 clientId
,但使用 HTTPS 時則無法使用。
HTTPS 訊息 URL
裝置和用戶端會將 POST 請求發佈至用戶端特定端點和特定主題的 URL,以發佈其訊息:
https://
IoT_data_endpoint
/topics/url_encoded_topic_name
?qos=1
-
IoT_data_endpoint
是 AWS 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 用戶端裝置傳送訊息
-
檢查 curl 版本。
-
在您的用戶端上,請在命令提示中執行此命令。
curl --help
在說明文字中,尋找 TLS 選項。您應該看到
--tlsv1.2
選項。 -
如果您看到
--tlsv1.2
選項,請繼續。 -
如果您沒有看到
--tlsv1.2
選項或您收到command not found
錯誤訊息,您可能需要在用戶端上更新或安裝 curl 或安裝openssl
,然後才能繼續進行。
-
-
在用戶端上安裝憑證。
複製您在 AWS IoT 主控台中註冊用戶端 (物件) 時建立的憑證檔案。在繼續之前,請確定您的用戶端上有這三個憑證檔案。
-
憑證授權機構憑證檔案 (此範例中的
Amazon-root-CA-1.pem
)。 -
用戶端的憑證檔案 (此範例中的
device.pem.crt
)。 -
用戶端的私密金鑰檔案 (此範例中的
private.pem.key
)。
-
-
建立 curl 命令列,取代您帳戶和系統的可取代值。
curl --tlsv1.2 \ --cacert
Amazon-root-CA-1.pem
\ --certdevice.pem.crt
\ --keyprivate.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\" }
" -
您要發佈的 HTTP POST 資料。在這種情況下,這是一個 JSON 字串,其內部引號用反斜線字元 (\) 逸出。
- "https://
IoT_data_endpoint
:8443/topics/topic
?qos=1" -
用戶端 AWS IoT 裝置資料端點的 URL,後面接著 HTTPS 連接埠
:8443
,接著關鍵字/topics/
和主題名稱topic
,在此情況下為 。指定服務品質作為查詢參數?qos=1
。
-
在 AWS IoT 主控台中開啟 MQTT 測試用戶端。
遵循使用 MQTT 用戶端檢視 AWS IoT MQTT 訊息中的指示並設定主控台,以訂閱用於 curl 命令中且主題名稱為
topic
的訊息,或使用#
的萬用字元主題篩選條件。 -
測試命令。
監控 AWS IoT 主控台測試用戶端中的主題時,請前往您的用戶端並發出您在步驟 3 中建立的 curl 命令列。您應該會在主控台中看到用戶端的訊息。
-