435 lines
14 KiB
C#
435 lines
14 KiB
C#
using RestSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
|
|
namespace Xylem.Common.Logic.SoftwareAccessHelper
|
|
{
|
|
/// <summary>
|
|
/// Web request
|
|
/// </summary>
|
|
public static class LocalWebRequest
|
|
{
|
|
/// <summary>
|
|
/// Web request with asynchronous operation and response code or exception response, json object input and/or dictionary
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="responseCode"></param>
|
|
/// <param name="ex"></param>
|
|
/// <param name="timeoutMs"></param>
|
|
/// <param name="json"></param>
|
|
/// <param name="header"></param>
|
|
/// <returns></returns>
|
|
public static Boolean PostRequestAsync(String url, ref Int32 responseCode, ref Exception ex, Int32? 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;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Web request with asynchronous operation and true/false return, json object input and/or dictionary
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="timeoutMs"></param>
|
|
/// <param name="json"></param>
|
|
/// <param name="header"></param>
|
|
/// <returns></returns>
|
|
public static Boolean PostRequestAsync(String url, Int32? 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)
|
|
{
|
|
// Log
|
|
}
|
|
return false;
|
|
|
|
}
|
|
/// <summary>
|
|
/// Web request with asynchronous operation with string return, json object input and/or dictionary
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="timeoutMs"></param>
|
|
/// <param name="json"></param>
|
|
/// <param name="header"></param>
|
|
/// <returns></returns>
|
|
public static String PostRequestAsyncAndGetContent(String url, Int32? 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)
|
|
{
|
|
// Log
|
|
}
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Web request with asynchronous operation, binary file byte array input, response string output
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="content"></param>
|
|
/// <param name="response"></param>
|
|
/// <param name="timeoutMs"></param>
|
|
/// <returns></returns>
|
|
public static Boolean PostBinaryFileRequestAsync(String url, String fileName, Byte[] content, out String response, Int32? timeoutMs = null)
|
|
{
|
|
try
|
|
{
|
|
|
|
var client = new RestClient(url);
|
|
//WebRequest.DefaultWebProxy = null;
|
|
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)
|
|
{
|
|
response = string.Empty;
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Web request with asynchronous operation, binary file input
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="content"></param>
|
|
/// <param name="timeoutMs"></param>
|
|
/// <returns></returns>
|
|
public static Boolean PostBinaryFileRequestAsync(String url, String fileName, Byte[] content, Int32? timeoutMs = null)
|
|
{
|
|
try
|
|
{
|
|
|
|
var client = new RestClient(url);
|
|
// WebRequest.DefaultWebProxy = null;
|
|
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)
|
|
{
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Web request with asynchronous operation
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="timeoutMs"></param>
|
|
/// <param name="files"></param>
|
|
/// <param name="header"></param>
|
|
/// <returns></returns>
|
|
public static IRestResponse PostBinaryRequestAndGetResponseAsync(String url, Int32? timeoutMs = null,
|
|
Dictionary<String, String> files = null, Dictionary<String, String> header = null)
|
|
{
|
|
try
|
|
{
|
|
|
|
var client = new RestClient(url);
|
|
//WebRequest.DefaultWebProxy = null;
|
|
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");
|
|
|
|
if (files != null)
|
|
{
|
|
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()
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Web request with asynchronous operation
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="timeoutMs"></param>
|
|
/// <param name="json"></param>
|
|
/// <param name="header"></param>
|
|
/// <returns></returns>
|
|
public static IRestResponse PostRequestAndGetResponseAsync(String url, Int32? timeoutMs = null, Object json = null,
|
|
Dictionary<String, String> header = null)
|
|
{
|
|
try
|
|
{
|
|
|
|
var client = new RestClient(url);
|
|
//WebRequest.DefaultWebProxy = null;
|
|
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 IRestResponse PutAsJson(this Object json, String url, Action<RestRequestHeaders> headersHandler = null, Int32? timeoutMs = null)
|
|
{
|
|
try
|
|
{
|
|
var client = new RestClient(url);
|
|
var request = new RestRequest(Method.PUT);
|
|
var headers = new RestRequestHeaders(request)
|
|
{
|
|
ContentType = "application/json",
|
|
Accept = "application/json"
|
|
};
|
|
|
|
headersHandler?.Invoke(headers);
|
|
|
|
if (json != null)
|
|
{
|
|
request.AddJsonBody(json);
|
|
}
|
|
|
|
ServicePointManager.Expect100Continue = false;
|
|
|
|
return client.Execute(request);
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
return new RestResponse
|
|
{
|
|
StatusCode = HttpStatusCode.InternalServerError,
|
|
Content = error.ToString()
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Web request
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="timeoutMs"></param>
|
|
/// <param name="header"></param>
|
|
/// <param name="isRetry"></param>
|
|
/// <returns></returns>
|
|
public static String GetRequest(String url, Int32? timeoutMs = null, Dictionary<String, String> header = null, Boolean isRetry = false)
|
|
{
|
|
Int32 temp;
|
|
var ret = GetRequestWithError(url, out temp, timeoutMs, header, isRetry);
|
|
if (temp != 200)
|
|
{
|
|
throw new ApplicationException($"WebRequest Status {temp}");
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Web request with asynchronous operation, error code response
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="errorCode"></param>
|
|
/// <param name="timeoutMs"></param>
|
|
/// <param name="header"></param>
|
|
/// <param name="isRetry"></param>
|
|
/// <returns></returns>
|
|
public static String GetRequestWithError(String url, out Int32 errorCode, Int32? timeoutMs = null, Dictionary<String, String> header = null, Boolean isRetry = false)
|
|
{
|
|
|
|
var client = new RestClient(url);
|
|
//WebRequest.DefaultWebProxy = null;
|
|
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;
|
|
}
|
|
}
|
|
}
|