You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
5.2 KiB
122 lines
5.2 KiB
using SSWMS.Common;
|
|
using System;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SSWMS.Server
|
|
{
|
|
class WebRequest
|
|
{
|
|
public static TimeSpan Timeout = new TimeSpan(0, 0, Convert.ToInt32(AppSettings.GetValue("RequestTimeoutSeconds")));
|
|
public static string ERPUrl = AppSettings.GetValue("ERPUrl");
|
|
public static string LocationID = AppSettings.GetValue("LocationID");
|
|
|
|
public static void Post(string uri, string body)
|
|
{
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
try
|
|
{
|
|
using (HttpClient httpClient = new HttpClient())
|
|
{
|
|
httpClient.Timeout = Timeout;
|
|
using (HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("Post"), uri))
|
|
{
|
|
request.Headers.TryAddWithoutValidation("User-Agent", "Warehouse Management System");
|
|
if (!string.IsNullOrWhiteSpace(body))
|
|
{
|
|
request.Headers.TryAddWithoutValidation("Content-Type", "application/json");
|
|
request.Content = new StringContent(body, Encoding.UTF8);
|
|
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
|
}
|
|
MainWindow.log.Debug($"WebRequest.Post {uri} {body} {httpClient.SendAsync(request).Result.Content.ReadAsStringAsync().Result}");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MainWindow.log.Error($"WebRequest.Post Exception {uri} {body} {ex.Message}");
|
|
}
|
|
});
|
|
}
|
|
|
|
public static bool Post(string uri, string body, out string content)
|
|
{
|
|
try
|
|
{
|
|
using (HttpClient httpClient = new HttpClient())
|
|
{
|
|
httpClient.Timeout = Timeout;
|
|
using (HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("Post"), uri))
|
|
{
|
|
request.Headers.TryAddWithoutValidation("User-Agent", "Warehouse Management System");
|
|
if (!string.IsNullOrWhiteSpace(body))
|
|
{
|
|
request.Headers.TryAddWithoutValidation("Content-Type", "application/json");
|
|
request.Content = new StringContent(body, Encoding.UTF8);
|
|
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
|
}
|
|
content = httpClient.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
|
|
MainWindow.log.Debug($"WebRequest.Post {uri} {body} {content}");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
content = ex.Message;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static void Get(string uri)
|
|
{
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
try
|
|
{
|
|
using (HttpClient httpClient = new HttpClient())
|
|
{
|
|
httpClient.Timeout = Timeout;
|
|
using (HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("Get"), uri))
|
|
{
|
|
request.Headers.TryAddWithoutValidation("User-Agent", "Warehouse Management System");
|
|
request.Headers.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
|
|
MainWindow.log.Debug($"WebRequest.Get {uri} {httpClient.SendAsync(request).Result.Content.ReadAsStringAsync().Result}");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MainWindow.log.Error($"WebRequest.Get Exception {uri} {ex.Message}");
|
|
}
|
|
});
|
|
}
|
|
|
|
public static bool Get(string uri, out string content)
|
|
{
|
|
try
|
|
{
|
|
using (HttpClient httpClient = new HttpClient())
|
|
{
|
|
httpClient.Timeout = Timeout;
|
|
using (HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("Get"), uri))
|
|
{
|
|
request.Headers.TryAddWithoutValidation("User-Agent", "Warehouse Management System");
|
|
request.Headers.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
|
|
content = httpClient.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
|
|
MainWindow.log.Debug($"WebRequest.Get {uri} {content}");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
content = ex.Message;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|