324 lines
9.3 KiB
C#
324 lines
9.3 KiB
C#
using RestSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
|
|
namespace Xylem.Common.Logic.SoftwareAccessHelper
|
|
{
|
|
public static class LocalWebRequest
|
|
{
|
|
|
|
|
|
|
|
|
|
public static bool PostRequestAsync(string Url, ref int responseCode, ref Exception ex, int? TimeoutMS = null, object json = null, Dictionary<string, string> Header = null)
|
|
{
|
|
try
|
|
{
|
|
var r = PostRequestAndGetResponseAsync(Url, TimeoutMS, json, Header);
|
|
responseCode = r.StatusCode.GetHashCode();
|
|
if (r.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
return true;
|
|
}
|
|
if (r.StatusCode == HttpStatusCode.InternalServerError)
|
|
{
|
|
ex = new Exception(r.Content);
|
|
}
|
|
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
ex = error;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
public static bool PostRequestAsync(string Url, int? TimeoutMS = null, object json = null, Dictionary<string, string> Header = null)
|
|
{
|
|
try
|
|
{
|
|
var r = PostRequestAndGetResponseAsync(Url, TimeoutMS, json, Header);
|
|
if (r.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
// Log
|
|
}
|
|
return false;
|
|
|
|
}
|
|
public static string PostRequestAsyncAndGetContent(string Url, int? TimeoutMS = null, object json = null, Dictionary<string, string> Header = null)
|
|
{
|
|
try
|
|
{
|
|
var r = PostRequestAndGetResponseAsync(Url, TimeoutMS, json, Header);
|
|
if (r.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
return r.Content;
|
|
}
|
|
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
// Log
|
|
}
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
public static bool PostBinaryFileRequestAsync(string Url, string FileName, byte[] Content, out string response, int? TimeoutMS = null)
|
|
{
|
|
try
|
|
{
|
|
|
|
var client = new RestClient(Url);
|
|
|
|
var request = new RestRequest(Method.POST);
|
|
|
|
ServicePointManager.Expect100Continue = false;
|
|
|
|
|
|
request.AddHeader("Content-Type", "multipart/form-data");
|
|
|
|
|
|
request.Files.Add(new FileParameter
|
|
{
|
|
Name = "file",
|
|
Writer = (s) => {
|
|
var stream = new MemoryStream(Content);
|
|
stream.CopyTo(s);
|
|
stream.Dispose();
|
|
},
|
|
FileName = FileName,
|
|
ContentType = "multipart/form-data",
|
|
ContentLength = Content.Length
|
|
});
|
|
|
|
|
|
var r = client.ExecuteAsPost(request, Method.POST.ToString());
|
|
response = r.Content;
|
|
return r.StatusCode == HttpStatusCode.OK;
|
|
|
|
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
response = string.Empty;
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static bool PostBinaryFileRequestAsync(string Url, string FileName, byte[] Content, int? TimeoutMS = null)
|
|
{
|
|
try
|
|
{
|
|
|
|
var client = new RestClient(Url);
|
|
|
|
var request = new RestRequest(Method.POST);
|
|
|
|
ServicePointManager.Expect100Continue = false;
|
|
|
|
|
|
request.AddHeader("Content-Type", "multipart/form-data");
|
|
|
|
|
|
request.Files.Add(new FileParameter
|
|
{
|
|
Name = "file",
|
|
Writer = (s) => {
|
|
var stream = new MemoryStream(Content);
|
|
stream.CopyTo(s);
|
|
stream.Dispose();
|
|
},
|
|
FileName = FileName,
|
|
ContentType = "multipart/form-data",
|
|
ContentLength = Content.Length
|
|
});
|
|
|
|
|
|
var r = client.ExecuteAsPost(request, Method.POST.ToString());
|
|
return r.StatusCode == HttpStatusCode.OK;
|
|
|
|
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static IRestResponse PostBinaryRequestAndGetResponseAsync(string Url, int? TimeoutMS = null, Dictionary<string, string> Files = null, Dictionary<string, string> Header = null)
|
|
{
|
|
try
|
|
{
|
|
|
|
var client = new RestClient(Url);
|
|
|
|
var request = new RestRequest(Method.POST);
|
|
|
|
ServicePointManager.Expect100Continue = false;
|
|
|
|
if (Header != null)
|
|
{
|
|
foreach (var item in Header)
|
|
{
|
|
if (item.Key != "Content-Type" && item.Key != "Accept")
|
|
{
|
|
request.AddHeader(item.Key, item.Value);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request.AddHeader("Content-Type", "multipart/form-data");
|
|
|
|
foreach (var item in Files)
|
|
{
|
|
request.AddFile(item.Key, item.Value);
|
|
}
|
|
|
|
|
|
|
|
var r = client.ExecuteAsPost(request, Method.POST.ToString());
|
|
return r;
|
|
|
|
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
return new RestResponse
|
|
{
|
|
StatusCode = HttpStatusCode.InternalServerError,
|
|
Content = error.ToString()
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static IRestResponse PostRequestAndGetResponseAsync(string Url, int? TimeoutMS = null, object json = null, Dictionary<string, string> Header = null)
|
|
{
|
|
try
|
|
{
|
|
|
|
var client = new RestClient(Url);
|
|
ServicePointManager.Expect100Continue = false;
|
|
var request = new RestRequest(Method.POST);
|
|
|
|
|
|
if (Header != null)
|
|
{
|
|
foreach (var item in Header)
|
|
{
|
|
if (item.Key != "Content-Type" && item.Key != "Accept")
|
|
{
|
|
request.AddHeader(item.Key, item.Value);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
request.AddHeader("Content-Type", "application/json");
|
|
request.AddHeader("Accept", "application/json");
|
|
|
|
if (json != null)
|
|
{
|
|
request.AddJsonBody(json);
|
|
}
|
|
|
|
var r = client.ExecuteAsPost(request, Method.POST.ToString());
|
|
return r;
|
|
|
|
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
return new RestResponse
|
|
{
|
|
StatusCode = HttpStatusCode.InternalServerError,
|
|
Content = error.ToString()
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
public static string GetRequest(string Url, int? TimeoutMS = null, Dictionary<string, string> Header = null, bool isRetry = false)
|
|
{
|
|
var tmp = 0;
|
|
var ret = GetRequestWithError(Url,out tmp, TimeoutMS ,Header , isRetry);
|
|
if (tmp != 200)
|
|
{
|
|
throw new ApplicationException($"WebRequest Status {tmp}");
|
|
}
|
|
return ret;
|
|
|
|
|
|
}
|
|
public static string GetRequestWithError(string Url, out int errorCode, int? TimeoutMS = null, Dictionary<string, string> Header = null, bool isRetry = false)
|
|
{
|
|
|
|
var client = new RestClient(Url);
|
|
|
|
var request = new RestRequest(Method.GET);
|
|
ServicePointManager.Expect100Continue = false;
|
|
if (!TimeoutMS.HasValue)
|
|
{
|
|
TimeoutMS = 800;
|
|
}
|
|
|
|
request.Timeout = TimeoutMS.Value;
|
|
|
|
if (Header != null)
|
|
{
|
|
foreach (var item in Header)
|
|
{
|
|
if (item.Key != "Accept")
|
|
{
|
|
request.AddHeader(item.Key, item.Value);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
request.AddHeader("Accept", "application/json");
|
|
|
|
var r = client.Get(request);
|
|
errorCode = r.StatusCode.GetHashCode();
|
|
if (r.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
return r.Content;
|
|
}
|
|
|
|
if (r.StatusCode != 0) return r.Content;
|
|
|
|
|
|
// Changed logic as it seems to be an endless loop THW 25-Mar-2021
|
|
//if (presult.Status == IPStatus.Success || !isRetry)
|
|
if (!isRetry)
|
|
{
|
|
return GetRequest(Url, TimeoutMS, Header, true);
|
|
}
|
|
// Removed throwing of exception as the caller cannot evaluate the result THW 25-Mar-2021
|
|
//throw new ApplicationException($"Timeout { r.StatusDescription} {Environment.NewLine} PingState: {presult.Status} {Environment.NewLine} RoundtripTime: {presult.RoundtripTime} ");
|
|
return r.Content;
|
|
}
|
|
}
|
|
}
|