Skip to content

Commit

Permalink
Post Json data
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanThuLab committed Aug 21, 2020
1 parent a4a4257 commit 99e02df
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
27 changes: 27 additions & 0 deletions CS029_Networking/3.HttpClientExampleSendAsync/.vscode/launch.json
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 CS029_Networking/3.HttpClientExampleSendAsync/.vscode/tasks.json
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"
}
]
}
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>
52 changes: 52 additions & 0 deletions CS029_Networking/3.HttpClientExampleSendAsync/Program.cs
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);
}
}
}

0 comments on commit 99e02df

Please sign in to comment.