Created
October 7, 2019 06:10
-
-
Save milovidov983/59a7410aefe5ffbef8904715f8a0f6ba to your computer and use it in GitHub Desktop.
CLI Curl csharp wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net; | |
namespace Helpers | |
{ | |
public class CurlRequestException:Exception{ | |
public CurlRequestException (string message):base(message) | |
{ | |
} | |
} | |
public class Curl | |
{ | |
public string Url | |
{ | |
get; | |
set; | |
} | |
private string _ct; | |
public string ContentType | |
{ | |
get | |
{ | |
return "-H \"Content-Type:" + _ct + "\""; | |
} | |
set | |
{ | |
_ct = value; | |
} | |
} | |
public IEnumerable<KeyValuePair<string, string>> Data | |
{ | |
get; | |
set; | |
} | |
private string _certFilePath; | |
public string CertFilePath | |
{ | |
get | |
{ | |
return "--cert " + _certFilePath; | |
} | |
set | |
{ | |
_certFilePath = value; | |
} | |
} | |
private string _keyFilePath; | |
public string KeyFilePath | |
{ | |
get | |
{ | |
return "--key " + _keyFilePath; | |
} | |
set | |
{ | |
_keyFilePath | |
= value; | |
} | |
} | |
private string _pass; | |
public string Pass | |
{ | |
get | |
{ | |
return "--pass " + _pass; | |
} | |
set | |
{ | |
_pass = value; | |
} | |
} | |
public string Other = "-X POST -k --fail --silent --show-error"; | |
private string _body; | |
public string Body | |
{ | |
get | |
{ | |
string result = null; | |
if (_body != null) | |
{ | |
result = _body; | |
} | |
else if (Data != null) | |
{ | |
result = String.Join("&", Data.Select(x => string.Format("{0}={1}", x.Key, WebUtility.UrlEncode(x.Value)))); | |
} | |
return result == null ? "" : ("--data \"" + result + "\""); | |
} | |
set | |
{ | |
_body = value; | |
} | |
} | |
public static async Task<string> Request(Curl option) | |
{ | |
using (var proc = new Process()) | |
{ | |
var args = string.Join(" ", option.Url, option.Body, option.ContentType, option.Other, option.CertFilePath, option.KeyFilePath, option.Pass); | |
proc.StartInfo = new ProcessStartInfo | |
{ | |
FileName = "curl", | |
Arguments = args, | |
RedirectStandardError = true, | |
RedirectStandardInput = true, | |
RedirectStandardOutput = true, | |
UseShellExecute = false, | |
WindowStyle = ProcessWindowStyle.Hidden, | |
CreateNoWindow = true | |
}; | |
proc.Start(); | |
var myWriter = proc.StandardInput; | |
var myOutput = proc.StandardOutput; | |
var myErrors = proc.StandardError; | |
var error = await myErrors.ReadToEndAsync().ConfigureAwait(false); | |
if (!string.IsNullOrWhiteSpace(error)) | |
{ | |
throw new CurlRequestException(error); | |
} | |
return await myOutput.ReadToEndAsync().ConfigureAwait(false); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment