回答編集履歴

1

見直しキャンペーン中

2023/07/28 16:07

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,385 +1,193 @@
1
1
  質問内容が消えていますが、運営が戻すはずなので回答しておきましょうか。
2
-
3
2
  もちろんsitumondaro1234さんご本人が戻してもいいですよ。
4
3
 
5
-
6
-
7
4
  C#で(VBは全然わかりません)、できるだけ元コードと同じ動作になるように移しました。
8
-
9
5
  確認に [httpbin.org](https://httpbin.org/) を使用させていただきました。
10
6
 
11
-
12
-
13
7
  変更点: 宛先変更・レスポンス表示
14
-
15
8
  ```Python
16
-
17
9
  import requests
18
-
19
10
  import json
20
11
 
21
12
 
22
-
23
-
24
-
25
13
  def get_csv(groupcodes, commandbytes):
26
-
27
14
  #url = 'http://192.168.14.100:7001/testapi/sendbroadcastcommand?groupcodes={}&commandbytes={}'. format(json.dumps(groupcodes), json.dumps(commandbytes))
28
-
29
15
  url = 'https://httpbin.org/post?groupcodes={}&commandbytes={}'. format(json.dumps(groupcodes), json.dumps(commandbytes))
30
-
31
16
  print(url)
32
17
 
33
-
34
-
35
18
  headers = {
36
-
37
19
  'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
38
-
39
20
  }
40
-
41
21
  try:
42
-
43
22
  response = requests.post(url=url, headers=headers)
44
23
 
45
-
46
-
47
24
  except Exception as e:
48
-
49
25
  print("POST 例外発生!")
50
-
51
26
  print(e)
52
-
53
27
  return -1
54
28
 
55
-
56
-
57
29
  if response.status_code != 200:
58
-
59
30
  print("POST 応答エラー")
60
-
61
31
  return -2
62
32
 
63
-
64
-
65
33
  print(json.dumps(dict(response.headers), indent=2))
66
-
67
34
  print(json.dumps(response.json(), indent=2))
68
35
 
69
36
 
70
-
71
-
72
-
73
37
  if __name__ == '__main__':
74
-
75
38
  grcd1 = ['A','B']
76
-
77
39
  cmdbyte1 = [4, 17, 0, 234]
78
40
 
79
-
80
-
81
41
  try:
82
-
83
42
  get_csv(grcd1, cmdbyte1)
84
-
85
43
  except Exception as e:
86
-
87
44
  print("get_scv()の中に例外発生!")
88
-
89
45
  print(e)
90
-
91
46
  ```
92
47
 
93
-
94
-
95
48
  出力: 一部伏せました(「***」部分)
96
-
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
+ }
97
81
  ```
98
-
99
- https://httpbin.org/post?groupcodes=["A", "B"]&commandbytes=[4, 17, 0, 234]
100
-
101
- {
102
-
103
- "Date": "Mon, 09 Aug 2021 08:36:50 GMT",
104
-
105
- "Content-Type": "application/json",
106
-
107
- "Content-Length": "603",
108
-
109
- "Connection": "keep-alive",
110
-
111
- "Server": "gunicorn/19.9.0",
112
-
113
- "Access-Control-Allow-Origin": "*",
114
-
115
- "Access-Control-Allow-Credentials": "true"
116
-
117
- }
118
-
119
- {
120
-
121
- "args": {
122
-
123
- "commandbytes": "[4, 17, 0, 234]",
124
-
125
- "groupcodes": "[\"A\", \"B\"]"
126
-
127
- },
128
-
129
- "data": "",
130
-
131
- "files": {},
132
-
133
- "form": {},
134
-
135
- "headers": {
136
-
137
- "Accept": "*/*",
138
-
139
- "Accept-Encoding": "gzip, deflate",
140
-
141
- "Content-Length": "0",
142
-
143
- "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
144
-
145
- "Host": "httpbin.org",
146
-
147
- "User-Agent": "python-requests/2.26.0",
148
-
149
- "X-Amzn-Trace-Id": ***
150
-
151
- },
152
-
153
- "json": null,
154
-
155
- "origin": ***,
156
-
157
- "url": "https://httpbin.org/post?groupcodes=[\"A\", \"B\"]&commandbytes=[4, 17, 0, 234]"
158
-
159
- }
160
-
161
- ```
162
-
163
-
164
82
 
165
83
  ---
166
84
 
167
-
168
-
169
- ```C#
85
+ ```cs
170
-
171
86
  using System;
172
-
173
87
  using System.Linq;
174
-
175
88
  using System.Net;
176
-
177
89
  using System.Net.Http;
178
-
179
90
  using System.Text.Json;
180
-
181
91
  using System.Threading.Tasks;
182
92
 
183
93
 
184
-
185
-
186
-
187
94
  async Task<int> GetCsvAsync(string[] groupCodes, byte[] commandBytes)
188
-
189
95
  {
190
-
191
96
  var gc = JsonSerializer.Serialize(groupCodes); // "[\"A\",\"B\"]"
192
-
193
97
  //var cb = JsonSerializer.Serialize(commandBytes); // "\"BBEA6g==\""
194
-
195
98
  var cb = JsonSerializer.Serialize(commandBytes.Select(x => (int)x).ToArray()); // "[4,17,0,234]" int[]ならOK なんで?w
196
99
 
197
-
198
-
199
100
  //var url = $"http://192.168.14.100:7001/testapi/sendbroadcastcommand?groupcodes={gc}&commandbytes={cb}";
200
-
201
101
  var url = $"https://httpbin.org/post?groupcodes={gc}&commandbytes={cb}";
202
-
203
102
  Console.WriteLine(url);
204
103
 
205
-
206
-
207
104
  var handler = new HttpClientHandler
208
-
209
105
  {
210
-
211
106
  AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
212
-
213
107
  };
214
-
215
108
  var client = new HttpClient(handler);
216
-
217
109
  var request = new HttpRequestMessage(HttpMethod.Post, url);
218
-
219
110
  request.Headers.Add("Accept", "*/*");
220
-
221
111
  request.Headers.Add("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
222
-
223
112
  request.Headers.Add("User-Agent", ".NET 5.0 HttpClient");
224
113
 
225
-
226
-
227
114
  HttpResponseMessage response;
228
-
229
115
  try
230
-
231
116
  {
232
-
233
117
  response = await client.SendAsync(request);
234
-
118
+ }
119
+ catch (Exception e)
120
+ {
121
+ Console.WriteLine("POST 例外発生!");
122
+ Console.WriteLine(e);
123
+ return -1;
235
124
  }
236
125
 
237
- catch (Exception e)
126
+ if (response.StatusCode != (HttpStatusCode)200) // HttpStatusCode.OK
238
-
239
127
  {
240
-
241
- Console.WriteLine("POST 例外発生!");
128
+ Console.WriteLine("POST 応答エラー");
242
-
243
- Console.WriteLine(e);
244
-
245
- return -1;
129
+ return -2;
246
-
247
130
  }
248
131
 
249
-
250
-
251
- if (response.StatusCode != (HttpStatusCode)200) // HttpStatusCode.OK
252
-
253
- {
254
-
255
- Console.WriteLine("POST 応答エラー");
256
-
257
- return -2;
258
-
259
- }
260
-
261
-
262
-
263
132
  Console.WriteLine(response.Headers);
264
-
265
133
  Console.WriteLine(response.Content.Headers);
266
134
 
267
-
268
-
269
135
  var content = await response.Content.ReadAsStringAsync();
270
-
271
136
  Console.WriteLine(content);
272
137
 
273
-
274
-
275
138
  return 0;
276
-
277
139
  }
278
140
 
279
141
 
280
-
281
-
282
-
283
142
  string[] grcd1 = { "A", "B" };
284
-
285
143
  byte[] cmdbyte1 = { 4, 17, 0, 234 };
286
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
+ ```
287
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
288
164
 
165
+ Content-Type: application/json
289
- try
166
+ Content-Length: 591
290
167
 
291
168
  {
292
-
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",
293
- await GetCsvAsync(grcd1, cmdbyte1);
183
+ "X-Amzn-Trace-Id": ***
294
-
184
+ },
185
+ "json": null,
186
+ "origin": ***,
187
+ "url": "https://httpbin.org/post?groupcodes=[\"A\",\"B\"]&commandbytes=[4,17,0,234]"
295
188
  }
296
-
297
- catch (Exception e)
298
-
299
- {
300
-
301
- Console.WriteLine("GetCsvAsync()の中に例外発生!");
302
-
303
- Console.WriteLine(e);
304
-
305
- }
306
-
307
189
  ```
308
190
 
309
-
310
-
311
- 出力: 一部伏せました(「***」部分)
312
-
313
- ```
314
-
315
- https://httpbin.org/post?groupcodes=["A","B"]&commandbytes=[4,17,0,234]
316
-
317
- Date: Mon, 09 Aug 2021 08:38:11 GMT
318
-
319
- Connection: keep-alive
320
-
321
- Server: gunicorn/19.9.0
322
-
323
- Access-Control-Allow-Origin: *
324
-
325
- Access-Control-Allow-Credentials: true
326
-
327
-
328
-
329
- Content-Type: application/json
330
-
331
- Content-Length: 591
332
-
333
-
334
-
335
- {
336
-
337
- "args": {
338
-
339
- "commandbytes": "[4,17,0,234]",
340
-
341
- "groupcodes": "[\"A\",\"B\"]"
342
-
343
- },
344
-
345
- "data": "",
346
-
347
- "files": {},
348
-
349
- "form": {},
350
-
351
- "headers": {
352
-
353
- "Accept": "*/*",
354
-
355
- "Accept-Encoding": "gzip, deflate",
356
-
357
- "Content-Length": "0",
358
-
359
- "Contenttype": "application/x-www-form-urlencoded;charset=UTF-8",
360
-
361
- "Host": "httpbin.org",
362
-
363
- "User-Agent": ".NET 5.0 HttpClient",
364
-
365
- "X-Amzn-Trace-Id": ***
366
-
367
- },
368
-
369
- "json": null,
370
-
371
- "origin": ***,
372
-
373
- "url": "https://httpbin.org/post?groupcodes=[\"A\",\"B\"]&commandbytes=[4,17,0,234]"
374
-
375
- }
376
-
377
- ```
378
-
379
-
380
-
381
191
  Top-level statementsを使用したので、.NET 5(C# 9.0)が必要です(使用できない場合はクラスとMainを書いてください)
382
-
383
192
  [最上位レベルのステートメント - Main メソッドを使用しないプログラム | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/csharp/fundamentals/program-structure/top-level-statements)
384
-
385
193
  [トップ レベル ステートメント - C# によるプログラミング入門 | ++C++; // 未確認飛行 C](https://ufcpp.net/study/csharp/misc/miscentrypoint/#top-level-statements)