refabrique : une librairie serveur
parent
1179767112
commit
9019f39ede
@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using Yavsc.Server.Model;
|
||||
|
||||
namespace Yavsc.Server.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Thanks to Stefan @ Stackoverflow
|
||||
/// </summary>
|
||||
public class RequestHelper
|
||||
{
|
||||
string WRPostMultipart(string url, Dictionary<string, object> parameters, string authorizationHeader = null)
|
||||
{
|
||||
|
||||
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
|
||||
byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
|
||||
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.ContentType = "multipart/form-data; boundary=" + boundary;
|
||||
request.Method = "POST";
|
||||
request.KeepAlive = true;
|
||||
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
|
||||
if (authorizationHeader != null)
|
||||
request.Headers["Authorization"] = authorizationHeader;
|
||||
if (parameters != null && parameters.Count > 0)
|
||||
{
|
||||
|
||||
using (Stream requestStream = request.GetRequestStream())
|
||||
{
|
||||
using (WebResponse response = request.GetResponse())
|
||||
{
|
||||
|
||||
|
||||
foreach (KeyValuePair<string, object> pair in parameters)
|
||||
{
|
||||
|
||||
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
|
||||
if (pair.Value is FormFile)
|
||||
{
|
||||
FormFile file = pair.Value as FormFile;
|
||||
string header = "Content-Disposition: form-data; name=\"" + pair.Key + "\"; filename=\"" + file.Name + "\"\r\nContent-Type: " + file.ContentType + "\r\n\r\n";
|
||||
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(header);
|
||||
requestStream.Write(bytes, 0, bytes.Length);
|
||||
byte[] buffer = new byte[32768];
|
||||
int bytesRead;
|
||||
if (file.Stream == null)
|
||||
{
|
||||
// upload from file
|
||||
using (FileStream fileStream = File.OpenRead(file.FilePath))
|
||||
{
|
||||
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
|
||||
requestStream.Write(buffer, 0, bytesRead);
|
||||
fileStream.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// upload from given stream
|
||||
while ((bytesRead = file.Stream.Read(buffer, 0, buffer.Length)) != 0)
|
||||
requestStream.Write(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string data = "Content-Disposition: form-data; name=\"" + pair.Key + "\"\r\n\r\n" + pair.Value;
|
||||
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
|
||||
requestStream.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
|
||||
requestStream.Write(trailer, 0, trailer.Length);
|
||||
requestStream.Close();
|
||||
|
||||
using (Stream responseStream = response.GetResponseStream())
|
||||
using (StreamReader reader = new StreamReader(responseStream))
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
} // end WebResponse response
|
||||
|
||||
} // end using requestStream
|
||||
|
||||
}
|
||||
else throw new ArgumentOutOfRangeException("no parameter found ");
|
||||
|
||||
}
|
||||
|
||||
public static async Task<string> PostMultipart(string url, FormFile[] formFiles, string access_token = null)
|
||||
{
|
||||
|
||||
if (formFiles != null && formFiles.Length > 0)
|
||||
{
|
||||
var client = new HttpClient();
|
||||
var formData = new MultipartFormDataContent();
|
||||
|
||||
if (access_token != null)
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token);
|
||||
foreach (var formFile in formFiles)
|
||||
{
|
||||
HttpContent fileStreamContent = new StreamContent(formFile.Stream);
|
||||
if (formFile.ContentType!=null)
|
||||
fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue(formFile.ContentType);
|
||||
else fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
|
||||
|
||||
// fileStreamContent.Headers.ContentDisposition = formFile.ContentDisposition!=null? new ContentDispositionHeaderValue(
|
||||
// formFile.ContentDisposition) : new ContentDispositionHeaderValue("form-data; name=\"file\"; filename=\"" + formFile.Name + "\"");
|
||||
fileStreamContent.Headers.Add("Content-Disposition", formFile.ContentDisposition);
|
||||
fileStreamContent.Headers.Add("Content-Length", formFile.Stream.Length.ToString());
|
||||
|
||||
//fileStreamContent.Headers.Add("FilePath", formFile.FilePath);
|
||||
|
||||
formData.Add(fileStreamContent, "file", formFile.Name);
|
||||
|
||||
}
|
||||
|
||||
var response = client.PostAsync(url, formData).Result;
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
} // end if formFiles != null
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
|
||||
using System.IO;
|
||||
using System.Net.Mime;
|
||||
|
||||
namespace Yavsc.Server.Model
|
||||
{
|
||||
public class FormFile
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
string contentDispositionString;
|
||||
public string ContentDisposition { get {
|
||||
return contentDispositionString;
|
||||
} set {
|
||||
ContentDisposition contentDisposition = new ContentDisposition(value);
|
||||
Name = contentDisposition.FileName;
|
||||
contentDispositionString = value;
|
||||
} }
|
||||
|
||||
public string ContentType { get; set; }
|
||||
|
||||
public string FilePath { get; set; }
|
||||
|
||||
public Stream Stream { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
"description": "Yavsc server common library",
|
||||
"authors": [
|
||||
"Paul Schneider <paul@pschneider.fr>"
|
||||
],
|
||||
"packOptions": {
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/pazof/yavsc"
|
||||
},
|
||||
"licenseUrl": "https://github.com/pazof/yavsc/blob/vnext/LICENSE",
|
||||
"requireLicenseAcceptance": true,
|
||||
"owners": [
|
||||
"Paul Schneider <paul@pschneider.fr>"
|
||||
],
|
||||
"summary": "Yet another very small company",
|
||||
"projectUrl": "http://yavsc.pschneider.fr",
|
||||
"tags": [
|
||||
"Blog",
|
||||
"Blog",
|
||||
"PoS",
|
||||
"Chat"
|
||||
]
|
||||
},
|
||||
"tooling": {
|
||||
"defaultNamespace": "Yavsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"Newtonsoft.Json": "9.0.1",
|
||||
"Yavsc.Abstract": {
|
||||
"target": "project",
|
||||
"type": "build"
|
||||
}
|
||||
},
|
||||
"frameworks": {
|
||||
"net451": {
|
||||
"frameworkAssemblies": {
|
||||
"System.ComponentModel.DataAnnotations": "4.0.0",
|
||||
"System.Json": "4.0.0",
|
||||
"System.Net": "4.0.0.0",
|
||||
"System.Net.Http": "4.0.0.0",
|
||||
"System.Xml": "4.0.0.0",
|
||||
"System": "4.0.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
{
|
||||
"locked": false,
|
||||
"version": 2,
|
||||
"targets": {
|
||||
".NETFramework,Version=v4.5.1": {
|
||||
"Newtonsoft.Json/9.0.1": {
|
||||
"type": "package",
|
||||
"compile": {
|
||||
"lib/net45/Newtonsoft.Json.dll": {}
|
||||
},
|
||||
"runtime": {
|
||||
"lib/net45/Newtonsoft.Json.dll": {}
|
||||
}
|
||||
},
|
||||
"Yavsc.Abstract/1.0.5": {
|
||||
"type": "project",
|
||||
"framework": ".NETFramework,Version=v4.5.1",
|
||||
"dependencies": {
|
||||
"Newtonsoft.Json": "9.0.1"
|
||||
},
|
||||
"frameworkAssemblies": [
|
||||
"System.ComponentModel.DataAnnotations",
|
||||
"System.Json"
|
||||
]
|
||||
}
|
||||
},
|
||||
".NETFramework,Version=v4.5.1/debian.9-x86": {
|
||||
"Newtonsoft.Json/9.0.1": {
|
||||
"type": "package",
|
||||
"compile": {
|
||||
"lib/net45/Newtonsoft.Json.dll": {}
|
||||
},
|
||||
"runtime": {
|
||||
"lib/net45/Newtonsoft.Json.dll": {}
|
||||
}
|
||||
},
|
||||
"Yavsc.Abstract/1.0.5": {
|
||||
"type": "project",
|
||||
"framework": ".NETFramework,Version=v4.5.1",
|
||||
"dependencies": {
|
||||
"Newtonsoft.Json": "9.0.1"
|
||||
},
|
||||
"frameworkAssemblies": [
|
||||
"System.ComponentModel.DataAnnotations",
|
||||
"System.Json"
|
||||
]
|
||||
}
|
||||
},
|
||||
".NETFramework,Version=v4.5.1/debian.9-x64": {
|
||||
"Newtonsoft.Json/9.0.1": {
|
||||
"type": "package",
|
||||
"compile": {
|
||||
"lib/net45/Newtonsoft.Json.dll": {}
|
||||
},
|
||||
"runtime": {
|
||||
"lib/net45/Newtonsoft.Json.dll": {}
|
||||
}
|
||||
},
|
||||
"Yavsc.Abstract/1.0.5": {
|
||||
"type": "project",
|
||||
"framework": ".NETFramework,Version=v4.5.1",
|
||||
"dependencies": {
|
||||
"Newtonsoft.Json": "9.0.1"
|
||||
},
|
||||
"frameworkAssemblies": [
|
||||
"System.ComponentModel.DataAnnotations",
|
||||
"System.Json"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"libraries": {
|
||||
"Yavsc.Abstract/1.0.5": {
|
||||
"type": "project",
|
||||
"path": "../Yavsc.Abstract/project.json"
|
||||
},
|
||||
"Newtonsoft.Json/9.0.1": {
|
||||
"type": "package",
|
||||
"sha512": "U82mHQSKaIk+lpSVCbWYKNavmNH1i5xrExDEquU1i6I5pV6UMOqRnJRSlKO3cMPfcpp0RgDY+8jUXHdQ4IfXvw==",
|
||||
"files": [
|
||||
"lib/net20/Newtonsoft.Json.dll",
|
||||
"lib/net20/Newtonsoft.Json.xml",
|
||||
"lib/net35/Newtonsoft.Json.dll",
|
||||
"lib/net35/Newtonsoft.Json.xml",
|
||||
"lib/net40/Newtonsoft.Json.dll",
|
||||
"lib/net40/Newtonsoft.Json.xml",
|
||||
"lib/net45/Newtonsoft.Json.dll",
|
||||
"lib/net45/Newtonsoft.Json.xml",
|
||||
"lib/netstandard1.0/Newtonsoft.Json.dll",
|
||||
"lib/netstandard1.0/Newtonsoft.Json.xml",
|
||||
"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"
|
||||
]
|
||||
}
|
||||
},
|
||||
"projectFileDependencyGroups": {
|
||||
"": [
|
||||
"Newtonsoft.Json >= 9.0.1",
|
||||
"Yavsc.Abstract "
|
||||
],
|
||||
".NETFramework,Version=v4.5.1": [
|
||||
"fx/System.ComponentModel.DataAnnotations >= 4.0.0",
|
||||
"fx/System.Json >= 4.0.0",
|
||||
"fx/System.Net >= 4.0.0",
|
||||
"fx/System.Xml >= 4.0.0",
|
||||
"fx/System >= 4.0.0"
|
||||
]
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
using System;
|
||||
|
||||
class YaDaemon
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello World!");
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp1.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
@ -1,5 +0,0 @@
|
||||
{
|
||||
"sdk": {
|
||||
"version": "1.0.0-rc4-004771"
|
||||
}
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
{
|
||||
"licence": "GNU GPL",
|
||||
"name": "testOauthClient",
|
||||
"version": "0.0.0",
|
||||
"devDependencies": {
|
||||
"gulp": "^3.9.0",
|
||||
"gulp-concat": "2.5.2",
|
||||
"gulp-cssmin": "0.1.7",
|
||||
"gulp-uglify": "1.2.0",
|
||||
"rimraf": "2.2.8"
|
||||
}
|
||||
}
|
||||
"licence": "GNU GPL v3",
|
||||
"name": "test-oauth-client",
|
||||
"version": "0.0.0",
|
||||
"devDependencies": {
|
||||
"gulp": "^3.9.1",
|
||||
"gulp-concat": "^2.6.1",
|
||||
"gulp-cssmin": "^0.2.0",
|
||||
"gulp-uglify": "^3.0.0",
|
||||
"rimraf": "^2.6.2"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue