refabrique : une librairie serveur
This commit is contained in:
@ -1,99 +0,0 @@
|
||||
//
|
||||
// PostJson.cs
|
||||
//
|
||||
// Author:
|
||||
// Paul Schneider <paulschneider@free.fr>
|
||||
//
|
||||
// Copyright (c) 2015 Paul Schneider
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Json;
|
||||
|
||||
namespace Yavsc.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple json post method.
|
||||
/// </summary>
|
||||
public class SimpleJsonPostMethod : IDisposable
|
||||
{
|
||||
private HttpWebRequest request=null;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the Yavsc.Helpers.SimpleJsonPostMethod class.
|
||||
/// </summary>
|
||||
/// <param name="pathToMethod">Path to method.</param>
|
||||
public SimpleJsonPostMethod (string pathToMethod, string authorizationHeader = null, string method = "POST")
|
||||
{
|
||||
request = (HttpWebRequest) WebRequest.Create (pathToMethod);
|
||||
request.Method = method;
|
||||
request.Accept = "application/json";
|
||||
request.ContentType = "application/json";
|
||||
request.SendChunked = true;
|
||||
request.TransferEncoding = "UTF-8";
|
||||
if (authorizationHeader!=null)
|
||||
request.Headers["Authorization"]=authorizationHeader;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
request.Abort();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invoke the specified query.
|
||||
/// </summary>
|
||||
/// <param name="query">Query.</param>
|
||||
public async Task<TAnswer> Invoke<TAnswer>(object query)
|
||||
{
|
||||
|
||||
using (Stream streamQuery = await request.GetRequestStreamAsync()) {
|
||||
using (StreamWriter writer = new StreamWriter(streamQuery)) {
|
||||
writer.Write (JsonConvert.SerializeObject(query));
|
||||
}}
|
||||
TAnswer ans = default (TAnswer);
|
||||
using (WebResponse response = await request.GetResponseAsync ()) {
|
||||
using (Stream responseStream = response.GetResponseStream ()) {
|
||||
using (StreamReader rdr = new StreamReader (responseStream)) {
|
||||
ans = (TAnswer) JsonConvert.DeserializeObject<TAnswer> (rdr.ReadToEnd ());
|
||||
}
|
||||
}
|
||||
response.Close();
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public async Task<JsonValue> InvokeJson(object query)
|
||||
{
|
||||
JsonValue jsonDoc=null;
|
||||
using (Stream streamQuery = request.GetRequestStream()) {
|
||||
using (StreamWriter writer = new StreamWriter(streamQuery)) {
|
||||
writer.Write (JsonConvert.SerializeObject(query));
|
||||
}}
|
||||
using (WebResponse response = request.GetResponse ()) {
|
||||
using (Stream stream = response.GetResponseStream ()) {
|
||||
if (stream.Length>0)
|
||||
jsonDoc = await Task.Run (() => JsonObject.Load (stream));
|
||||
}
|
||||
response.Close();
|
||||
}
|
||||
return jsonDoc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,13 +23,11 @@
|
||||
"Chat"
|
||||
]
|
||||
},
|
||||
"projectUrl": "",
|
||||
"licenseUrl": "",
|
||||
"tooling": {
|
||||
"defaultNamespace": "Yavsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"Newtonsoft.Json": "10.0.2"
|
||||
"Newtonsoft.Json": "9.0.1"
|
||||
},
|
||||
"frameworks": {
|
||||
"net451": {
|
||||
|
@ -3,7 +3,7 @@
|
||||
"version": 2,
|
||||
"targets": {
|
||||
".NETFramework,Version=v4.5.1": {
|
||||
"Newtonsoft.Json/10.0.2": {
|
||||
"Newtonsoft.Json/9.0.1": {
|
||||
"type": "package",
|
||||
"compile": {
|
||||
"lib/net45/Newtonsoft.Json.dll": {}
|
||||
@ -14,7 +14,7 @@
|
||||
}
|
||||
},
|
||||
".NETFramework,Version=v4.5.1/debian.9-x86": {
|
||||
"Newtonsoft.Json/10.0.2": {
|
||||
"Newtonsoft.Json/9.0.1": {
|
||||
"type": "package",
|
||||
"compile": {
|
||||
"lib/net45/Newtonsoft.Json.dll": {}
|
||||
@ -25,7 +25,7 @@
|
||||
}
|
||||
},
|
||||
".NETFramework,Version=v4.5.1/debian.9-x64": {
|
||||
"Newtonsoft.Json/10.0.2": {
|
||||
"Newtonsoft.Json/9.0.1": {
|
||||
"type": "package",
|
||||
"compile": {
|
||||
"lib/net45/Newtonsoft.Json.dll": {}
|
||||
@ -37,9 +37,9 @@
|
||||
}
|
||||
},
|
||||
"libraries": {
|
||||
"Newtonsoft.Json/10.0.2": {
|
||||
"Newtonsoft.Json/9.0.1": {
|
||||
"type": "package",
|
||||
"sha512": "iwElSU2IXmwGvytJsezyDML2ZWDkG2JzTYzlU/BNlmzMdlmRvbnwITsGGY74gwVEpDli1UdOLkMT7/3jxWvXzA==",
|
||||
"sha512": "U82mHQSKaIk+lpSVCbWYKNavmNH1i5xrExDEquU1i6I5pV6UMOqRnJRSlKO3cMPfcpp0RgDY+8jUXHdQ4IfXvw==",
|
||||
"files": [
|
||||
"lib/net20/Newtonsoft.Json.dll",
|
||||
"lib/net20/Newtonsoft.Json.xml",
|
||||
@ -51,15 +51,12 @@
|
||||
"lib/net45/Newtonsoft.Json.xml",
|
||||
"lib/netstandard1.0/Newtonsoft.Json.dll",
|
||||
"lib/netstandard1.0/Newtonsoft.Json.xml",
|
||||
"lib/netstandard1.3/Newtonsoft.Json.dll",
|
||||
"lib/netstandard1.3/Newtonsoft.Json.xml",
|
||||
"lib/portable-net40+sl5+win8+wpa81+wp8/Newtonsoft.Json.dll",
|
||||
"lib/portable-net40+sl5+win8+wpa81+wp8/Newtonsoft.Json.xml",
|
||||
"lib/portable-net45+win8+wpa81+wp8/Newtonsoft.Json.dll",
|
||||
"lib/portable-net45+win8+wpa81+wp8/Newtonsoft.Json.xml",
|
||||
"LICENSE.md",
|
||||
"Newtonsoft.Json.10.0.2.nupkg",
|
||||
"Newtonsoft.Json.10.0.2.nupkg.sha512",
|
||||
"lib/portable-net40+sl5+wp80+win8+wpa81/Newtonsoft.Json.dll",
|
||||
"lib/portable-net40+sl5+wp80+win8+wpa81/Newtonsoft.Json.xml",
|
||||
"lib/portable-net45+wp80+win8+wpa81/Newtonsoft.Json.dll",
|
||||
"lib/portable-net45+wp80+win8+wpa81/Newtonsoft.Json.xml",
|
||||
"Newtonsoft.Json.9.0.1.nupkg",
|
||||
"Newtonsoft.Json.9.0.1.nupkg.sha512",
|
||||
"Newtonsoft.Json.nuspec",
|
||||
"tools/install.ps1"
|
||||
]
|
||||
@ -67,7 +64,7 @@
|
||||
},
|
||||
"projectFileDependencyGroups": {
|
||||
"": [
|
||||
"Newtonsoft.Json >= 10.0.2"
|
||||
"Newtonsoft.Json >= 9.0.1"
|
||||
],
|
||||
".NETFramework,Version=v4.5.1": [
|
||||
"fx/System.ComponentModel.DataAnnotations >= 4.0.0",
|
||||
|
Reference in New Issue
Block a user