From 99e02dfd7a8164afab2bc51cf405563d7e0c2584 Mon Sep 17 00:00:00 2001 From: XuanThuLab Date: Fri, 21 Aug 2020 21:38:40 +0700 Subject: [PATCH] Post Json data --- .../.vscode/launch.json | 27 ++++++++++ .../.vscode/tasks.json | 42 +++++++++++++++ .../HttpClientExample.csproj | 8 +++ .../3.HttpClientExampleSendAsync/Program.cs | 52 +++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 CS029_Networking/3.HttpClientExampleSendAsync/.vscode/launch.json create mode 100644 CS029_Networking/3.HttpClientExampleSendAsync/.vscode/tasks.json create mode 100755 CS029_Networking/3.HttpClientExampleSendAsync/HttpClientExample.csproj create mode 100755 CS029_Networking/3.HttpClientExampleSendAsync/Program.cs diff --git a/CS029_Networking/3.HttpClientExampleSendAsync/.vscode/launch.json b/CS029_Networking/3.HttpClientExampleSendAsync/.vscode/launch.json new file mode 100644 index 0000000..7970ee4 --- /dev/null +++ b/CS029_Networking/3.HttpClientExampleSendAsync/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/HttpClientExample.dll", + "args": [], + "cwd": "${workspaceFolder}", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} \ No newline at end of file diff --git a/CS029_Networking/3.HttpClientExampleSendAsync/.vscode/tasks.json b/CS029_Networking/3.HttpClientExampleSendAsync/.vscode/tasks.json new file mode 100644 index 0000000..0ee9d18 --- /dev/null +++ b/CS029_Networking/3.HttpClientExampleSendAsync/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/HttpClientExample.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/HttpClientExample.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/HttpClientExample.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/CS029_Networking/3.HttpClientExampleSendAsync/HttpClientExample.csproj b/CS029_Networking/3.HttpClientExampleSendAsync/HttpClientExample.csproj new file mode 100755 index 0000000..c73e0d1 --- /dev/null +++ b/CS029_Networking/3.HttpClientExampleSendAsync/HttpClientExample.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/CS029_Networking/3.HttpClientExampleSendAsync/Program.cs b/CS029_Networking/3.HttpClientExampleSendAsync/Program.cs new file mode 100755 index 0000000..04db67a --- /dev/null +++ b/CS029_Networking/3.HttpClientExampleSendAsync/Program.cs @@ -0,0 +1,52 @@ +using System; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; +using System.Threading.Tasks; +using System.IO; +using System.Text; +namespace HttpClientExample +{ + public class ViduHttpClient { + HttpClient _httpClient = null; + public HttpClient httpClient => _httpClient ?? (new HttpClient()); + + // Post Json Data + public async Task SendAsyncJson(string url, string json) + { + Console.WriteLine($"Starting connect {url}"); + try { + + var request = new HttpRequestMessage(HttpMethod.Post, url); + HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json"); + request.Content = httpContent; + var response = await httpClient.SendAsync(request); + var rcontent = await response.Content.ReadAsStringAsync(); + return rcontent; + + } catch (Exception e) { + Console.WriteLine(e.Message); + throw e; + } + } + } + class Program + { + static void Main(string[] args) + { + var url = "https://xuanthulab.net/api/"; + var json =@" + { + ""id"":""1"", + ""method"":""timestampToDate"", + ""params"": {""routin"":""UnixTime"", ""timestamp"":""1483228800""} + }"; + ViduHttpClient vidu = new ViduHttpClient(); + var task = vidu.SendAsyncJson(url, json); + task.Wait(); + Console.WriteLine(task.Result); + } + } +}