-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
XuanThuLab
committed
Aug 21, 2020
1 parent
a4a4257
commit 99e02df
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
CS029_Networking/3.HttpClientExampleSendAsync/.vscode/launch.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}" | ||
} | ||
] | ||
} |
42 changes: 42 additions & 0 deletions
42
CS029_Networking/3.HttpClientExampleSendAsync/.vscode/tasks.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
CS029_Networking/3.HttpClientExampleSendAsync/HttpClientExample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> 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); | ||
} | ||
} | ||
} |