Esempio n. 1
0
        private void Send(string method, IotHubRequestList aRequests)
        {
            // シリアライズ
            string json = JsonConvert.SerializeObject(aRequests, Formatting.None);

            // 送信する文字列をバイト型配列に変換
            byte[] putDataBytes = System.Text.Encoding.UTF8.GetBytes(json);

            // WebRequest の作成
            System.Net.WebRequest webReq = System.Net.WebRequest.Create(IOT_HUB_URL);
            // メソッドに PUT を指定
            webReq.Method = method;

            // ContentType を設定
            webReq.ContentType = SEND_CONTENT_TYPE;

            // API_KEY のBasic認証を追加
            webReq.Headers.Add(CommandConst.API_KEY_HEADER, CommandConst.API_KEY_VALUE);

            // 送信するデータの長さを指定
            webReq.ContentLength = putDataBytes.Length;

            // データを送信するためのStreamを取得
            System.IO.Stream reqStream = webReq.GetRequestStream();
            // 送信するデータを書き込む
            reqStream.Write(putDataBytes, 0, putDataBytes.Length);
            reqStream.Close();

            return;
        }
        private void Send(string method, IotHubRequestList aRequests)
        {
            // シリアライズ
            string json = JsonConvert.SerializeObject(aRequests, Formatting.None);

            // 処理したリストのデータは削除しておく。
            aRequests.requestList[0].commandList.RemoveAt(requestListCount);
            aRequests.requestList.RemoveAt(0);

            // 送信する文字列をバイト型配列に変換。エンコードは UTF-8 指定。
            byte[] putDataBytes = System.Text.Encoding.UTF8.GetBytes(json);

            // WebRequest の作成。
            System.Net.WebRequest webReq = System.Net.WebRequest.Create(IOT_HUB_URL);

            // メソッドを指定。
            webReq.Method = method;

            // ContentType を設定。
            webReq.ContentType = SEND_CONTENT_TYPE;

            // API_KEY の BASIC 認証を追加。
            webReq.Headers.Add(API_KEY_HEADER, API_KEY_VALUE);

            // 送信するデータの長さを指定。
            webReq.ContentLength = putDataBytes.Length;

            // データを送信するための Stream を取得。
            System.IO.Stream reqStream = webReq.GetRequestStream();

            // 送信するデータを書き込むんで送信。 Stream を閉じる。
            reqStream.Write(putDataBytes, 0, putDataBytes.Length);
            reqStream.Close();

            return;
        }