teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/28 16:07

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,193 +1,193 @@
1
- 質問内容が消えていますが、運営が戻すはずなので回答しておきましょうか。
2
- もちろんsitumondaro1234さんご本人が戻してもいいですよ。
3
-
4
- C#で(VBは全然わかりません)、できるだけ元コードと同じ動作になるように移しました。
5
- 確認に [httpbin.org](https://httpbin.org/) を使用させていただきました。
6
-
7
- 変更点: 宛先変更・レスポンス表示
8
- ```Python
9
- import requests
10
- import json
11
-
12
-
13
- def get_csv(groupcodes, commandbytes):
14
- #url = 'http://192.168.14.100:7001/testapi/sendbroadcastcommand?groupcodes={}&commandbytes={}'. format(json.dumps(groupcodes), json.dumps(commandbytes))
15
- url = 'https://httpbin.org/post?groupcodes={}&commandbytes={}'. format(json.dumps(groupcodes), json.dumps(commandbytes))
16
- print(url)
17
-
18
- headers = {
19
- 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
20
- }
21
- try:
22
- response = requests.post(url=url, headers=headers)
23
-
24
- except Exception as e:
25
- print("POST 例外発生!")
26
- print(e)
27
- return -1
28
-
29
- if response.status_code != 200:
30
- print("POST 応答エラー")
31
- return -2
32
-
33
- print(json.dumps(dict(response.headers), indent=2))
34
- print(json.dumps(response.json(), indent=2))
35
-
36
-
37
- if __name__ == '__main__':
38
- grcd1 = ['A','B']
39
- cmdbyte1 = [4, 17, 0, 234]
40
-
41
- try:
42
- get_csv(grcd1, cmdbyte1)
43
- except Exception as e:
44
- print("get_scv()の中に例外発生!")
45
- print(e)
46
- ```
47
-
48
- 出力: 一部伏せました(「***」部分)
49
- ```
50
- https://httpbin.org/post?groupcodes=["A", "B"]&commandbytes=[4, 17, 0, 234]
51
- {
52
- "Date": "Mon, 09 Aug 2021 08:36:50 GMT",
53
- "Content-Type": "application/json",
54
- "Content-Length": "603",
55
- "Connection": "keep-alive",
56
- "Server": "gunicorn/19.9.0",
57
- "Access-Control-Allow-Origin": "*",
58
- "Access-Control-Allow-Credentials": "true"
59
- }
60
- {
61
- "args": {
62
- "commandbytes": "[4, 17, 0, 234]",
63
- "groupcodes": "[\"A\", \"B\"]"
64
- },
65
- "data": "",
66
- "files": {},
67
- "form": {},
68
- "headers": {
69
- "Accept": "*/*",
70
- "Accept-Encoding": "gzip, deflate",
71
- "Content-Length": "0",
72
- "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
73
- "Host": "httpbin.org",
74
- "User-Agent": "python-requests/2.26.0",
75
- "X-Amzn-Trace-Id": ***
76
- },
77
- "json": null,
78
- "origin": ***,
79
- "url": "https://httpbin.org/post?groupcodes=[\"A\", \"B\"]&commandbytes=[4, 17, 0, 234]"
80
- }
81
- ```
82
-
83
- ---
84
-
85
- ```C#
86
- using System;
87
- using System.Linq;
88
- using System.Net;
89
- using System.Net.Http;
90
- using System.Text.Json;
91
- using System.Threading.Tasks;
92
-
93
-
94
- async Task<int> GetCsvAsync(string[] groupCodes, byte[] commandBytes)
95
- {
96
- var gc = JsonSerializer.Serialize(groupCodes); // "[\"A\",\"B\"]"
97
- //var cb = JsonSerializer.Serialize(commandBytes); // "\"BBEA6g==\""
98
- var cb = JsonSerializer.Serialize(commandBytes.Select(x => (int)x).ToArray()); // "[4,17,0,234]" int[]ならOK なんで?w
99
-
100
- //var url = $"http://192.168.14.100:7001/testapi/sendbroadcastcommand?groupcodes={gc}&commandbytes={cb}";
101
- var url = $"https://httpbin.org/post?groupcodes={gc}&commandbytes={cb}";
102
- Console.WriteLine(url);
103
-
104
- var handler = new HttpClientHandler
105
- {
106
- AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
107
- };
108
- var client = new HttpClient(handler);
109
- var request = new HttpRequestMessage(HttpMethod.Post, url);
110
- request.Headers.Add("Accept", "*/*");
111
- request.Headers.Add("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
112
- request.Headers.Add("User-Agent", ".NET 5.0 HttpClient");
113
-
114
- HttpResponseMessage response;
115
- try
116
- {
117
- response = await client.SendAsync(request);
118
- }
119
- catch (Exception e)
120
- {
121
- Console.WriteLine("POST 例外発生!");
122
- Console.WriteLine(e);
123
- return -1;
124
- }
125
-
126
- if (response.StatusCode != (HttpStatusCode)200) // HttpStatusCode.OK
127
- {
128
- Console.WriteLine("POST 応答エラー");
129
- return -2;
130
- }
131
-
132
- Console.WriteLine(response.Headers);
133
- Console.WriteLine(response.Content.Headers);
134
-
135
- var content = await response.Content.ReadAsStringAsync();
136
- Console.WriteLine(content);
137
-
138
- return 0;
139
- }
140
-
141
-
142
- string[] grcd1 = { "A", "B" };
143
- byte[] cmdbyte1 = { 4, 17, 0, 234 };
144
-
145
- try
146
- {
147
- await GetCsvAsync(grcd1, cmdbyte1);
148
- }
149
- catch (Exception e)
150
- {
151
- Console.WriteLine("GetCsvAsync()の中に例外発生!");
152
- Console.WriteLine(e);
153
- }
154
- ```
155
-
156
- 出力: 一部伏せました(「***」部分)
157
- ```
158
- https://httpbin.org/post?groupcodes=["A","B"]&commandbytes=[4,17,0,234]
159
- Date: Mon, 09 Aug 2021 08:38:11 GMT
160
- Connection: keep-alive
161
- Server: gunicorn/19.9.0
162
- Access-Control-Allow-Origin: *
163
- Access-Control-Allow-Credentials: true
164
-
165
- Content-Type: application/json
166
- Content-Length: 591
167
-
168
- {
169
- "args": {
170
- "commandbytes": "[4,17,0,234]",
171
- "groupcodes": "[\"A\",\"B\"]"
172
- },
173
- "data": "",
174
- "files": {},
175
- "form": {},
176
- "headers": {
177
- "Accept": "*/*",
178
- "Accept-Encoding": "gzip, deflate",
179
- "Content-Length": "0",
180
- "Contenttype": "application/x-www-form-urlencoded;charset=UTF-8",
181
- "Host": "httpbin.org",
182
- "User-Agent": ".NET 5.0 HttpClient",
183
- "X-Amzn-Trace-Id": ***
184
- },
185
- "json": null,
186
- "origin": ***,
187
- "url": "https://httpbin.org/post?groupcodes=[\"A\",\"B\"]&commandbytes=[4,17,0,234]"
188
- }
189
- ```
190
-
191
- Top-level statementsを使用したので、.NET 5(C# 9.0)が必要です(使用できない場合はクラスとMainを書いてください)
192
- [最上位レベルのステートメント - Main メソッドを使用しないプログラム | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/csharp/fundamentals/program-structure/top-level-statements)
1
+ 質問内容が消えていますが、運営が戻すはずなので回答しておきましょうか。
2
+ もちろんsitumondaro1234さんご本人が戻してもいいですよ。
3
+
4
+ C#で(VBは全然わかりません)、できるだけ元コードと同じ動作になるように移しました。
5
+ 確認に [httpbin.org](https://httpbin.org/) を使用させていただきました。
6
+
7
+ 変更点: 宛先変更・レスポンス表示
8
+ ```Python
9
+ import requests
10
+ import json
11
+
12
+
13
+ def get_csv(groupcodes, commandbytes):
14
+ #url = 'http://192.168.14.100:7001/testapi/sendbroadcastcommand?groupcodes={}&commandbytes={}'. format(json.dumps(groupcodes), json.dumps(commandbytes))
15
+ url = 'https://httpbin.org/post?groupcodes={}&commandbytes={}'. format(json.dumps(groupcodes), json.dumps(commandbytes))
16
+ print(url)
17
+
18
+ headers = {
19
+ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
20
+ }
21
+ try:
22
+ response = requests.post(url=url, headers=headers)
23
+
24
+ except Exception as e:
25
+ print("POST 例外発生!")
26
+ print(e)
27
+ return -1
28
+
29
+ if response.status_code != 200:
30
+ print("POST 応答エラー")
31
+ return -2
32
+
33
+ print(json.dumps(dict(response.headers), indent=2))
34
+ print(json.dumps(response.json(), indent=2))
35
+
36
+
37
+ if __name__ == '__main__':
38
+ grcd1 = ['A','B']
39
+ cmdbyte1 = [4, 17, 0, 234]
40
+
41
+ try:
42
+ get_csv(grcd1, cmdbyte1)
43
+ except Exception as e:
44
+ print("get_scv()の中に例外発生!")
45
+ print(e)
46
+ ```
47
+
48
+ 出力: 一部伏せました(「***」部分)
49
+ ```json
50
+ https://httpbin.org/post?groupcodes=["A", "B"]&commandbytes=[4, 17, 0, 234]
51
+ {
52
+ "Date": "Mon, 09 Aug 2021 08:36:50 GMT",
53
+ "Content-Type": "application/json",
54
+ "Content-Length": "603",
55
+ "Connection": "keep-alive",
56
+ "Server": "gunicorn/19.9.0",
57
+ "Access-Control-Allow-Origin": "*",
58
+ "Access-Control-Allow-Credentials": "true"
59
+ }
60
+ {
61
+ "args": {
62
+ "commandbytes": "[4, 17, 0, 234]",
63
+ "groupcodes": "[\"A\", \"B\"]"
64
+ },
65
+ "data": "",
66
+ "files": {},
67
+ "form": {},
68
+ "headers": {
69
+ "Accept": "*/*",
70
+ "Accept-Encoding": "gzip, deflate",
71
+ "Content-Length": "0",
72
+ "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
73
+ "Host": "httpbin.org",
74
+ "User-Agent": "python-requests/2.26.0",
75
+ "X-Amzn-Trace-Id": ***
76
+ },
77
+ "json": null,
78
+ "origin": ***,
79
+ "url": "https://httpbin.org/post?groupcodes=[\"A\", \"B\"]&commandbytes=[4, 17, 0, 234]"
80
+ }
81
+ ```
82
+
83
+ ---
84
+
85
+ ```cs
86
+ using System;
87
+ using System.Linq;
88
+ using System.Net;
89
+ using System.Net.Http;
90
+ using System.Text.Json;
91
+ using System.Threading.Tasks;
92
+
93
+
94
+ async Task<int> GetCsvAsync(string[] groupCodes, byte[] commandBytes)
95
+ {
96
+ var gc = JsonSerializer.Serialize(groupCodes); // "[\"A\",\"B\"]"
97
+ //var cb = JsonSerializer.Serialize(commandBytes); // "\"BBEA6g==\""
98
+ var cb = JsonSerializer.Serialize(commandBytes.Select(x => (int)x).ToArray()); // "[4,17,0,234]" int[]ならOK なんで?w
99
+
100
+ //var url = $"http://192.168.14.100:7001/testapi/sendbroadcastcommand?groupcodes={gc}&commandbytes={cb}";
101
+ var url = $"https://httpbin.org/post?groupcodes={gc}&commandbytes={cb}";
102
+ Console.WriteLine(url);
103
+
104
+ var handler = new HttpClientHandler
105
+ {
106
+ AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
107
+ };
108
+ var client = new HttpClient(handler);
109
+ var request = new HttpRequestMessage(HttpMethod.Post, url);
110
+ request.Headers.Add("Accept", "*/*");
111
+ request.Headers.Add("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
112
+ request.Headers.Add("User-Agent", ".NET 5.0 HttpClient");
113
+
114
+ HttpResponseMessage response;
115
+ try
116
+ {
117
+ response = await client.SendAsync(request);
118
+ }
119
+ catch (Exception e)
120
+ {
121
+ Console.WriteLine("POST 例外発生!");
122
+ Console.WriteLine(e);
123
+ return -1;
124
+ }
125
+
126
+ if (response.StatusCode != (HttpStatusCode)200) // HttpStatusCode.OK
127
+ {
128
+ Console.WriteLine("POST 応答エラー");
129
+ return -2;
130
+ }
131
+
132
+ Console.WriteLine(response.Headers);
133
+ Console.WriteLine(response.Content.Headers);
134
+
135
+ var content = await response.Content.ReadAsStringAsync();
136
+ Console.WriteLine(content);
137
+
138
+ return 0;
139
+ }
140
+
141
+
142
+ string[] grcd1 = { "A", "B" };
143
+ byte[] cmdbyte1 = { 4, 17, 0, 234 };
144
+
145
+ try
146
+ {
147
+ await GetCsvAsync(grcd1, cmdbyte1);
148
+ }
149
+ catch (Exception e)
150
+ {
151
+ Console.WriteLine("GetCsvAsync()の中に例外発生!");
152
+ Console.WriteLine(e);
153
+ }
154
+ ```
155
+
156
+ 出力: 一部伏せました(「***」部分)
157
+ ```json
158
+ https://httpbin.org/post?groupcodes=["A","B"]&commandbytes=[4,17,0,234]
159
+ Date: Mon, 09 Aug 2021 08:38:11 GMT
160
+ Connection: keep-alive
161
+ Server: gunicorn/19.9.0
162
+ Access-Control-Allow-Origin: *
163
+ Access-Control-Allow-Credentials: true
164
+
165
+ Content-Type: application/json
166
+ Content-Length: 591
167
+
168
+ {
169
+ "args": {
170
+ "commandbytes": "[4,17,0,234]",
171
+ "groupcodes": "[\"A\",\"B\"]"
172
+ },
173
+ "data": "",
174
+ "files": {},
175
+ "form": {},
176
+ "headers": {
177
+ "Accept": "*/*",
178
+ "Accept-Encoding": "gzip, deflate",
179
+ "Content-Length": "0",
180
+ "Contenttype": "application/x-www-form-urlencoded;charset=UTF-8",
181
+ "Host": "httpbin.org",
182
+ "User-Agent": ".NET 5.0 HttpClient",
183
+ "X-Amzn-Trace-Id": ***
184
+ },
185
+ "json": null,
186
+ "origin": ***,
187
+ "url": "https://httpbin.org/post?groupcodes=[\"A\",\"B\"]&commandbytes=[4,17,0,234]"
188
+ }
189
+ ```
190
+
191
+ Top-level statementsを使用したので、.NET 5(C# 9.0)が必要です(使用できない場合はクラスとMainを書いてください)
192
+ [最上位レベルのステートメント - Main メソッドを使用しないプログラム | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/csharp/fundamentals/program-structure/top-level-statements)
193
193
  [トップ レベル ステートメント - C# によるプログラミング入門 | ++C++; // 未確認飛行 C](https://ufcpp.net/study/csharp/misc/miscentrypoint/#top-level-statements)