public
async Task<
bool> CreateLogStore(
string project,
string logStore)
{
using ( var client = new HttpClient())
{
var time = DateTime.Now;
string body = Newtonsoft.Json.JsonConvert.SerializeObject( new LogBody() { logstoreName = logStore, ttl = 1, shardCount = 2 });
string date = FormatRfc822Date(time);
string contentMD5 = MD5Encrypt(body);
string signString = Sign( " POST\n " + contentMD5 + " \napplication/json\n " + date + " \nx-log-apiversion:0.6.0\nx-log-signaturemethod:hmac-sha1\n/logstores ", this.accessKeySecret);
HttpContent content = new StringContent(body);
string sign = string.Format( " {0}:{1} ", accessKeyId, signString);
client.DefaultRequestHeaders.Add( " x-log-apiversion ", " 0.6.0 ");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( " LOG ", sign);
client.DefaultRequestHeaders.Add( " host ", project + " . " + endpoint);
client.DefaultRequestHeaders.Add( " Date ", date);
client.DefaultRequestHeaders.Add( " x-log-signaturemethod ", " hmac-sha1 ");
client.DefaultRequestHeaders.Add( " ContentMD5 ", contentMD5);
content.Headers.ContentType = new MediaTypeHeaderValue( " application/json ");
try
{
var res = await client.PostAsync( string.Format( " http://{0}.{1}/logstores ", project, endpoint), content).ConfigureAwait( false);
return res != null && res.StatusCode == System.Net.HttpStatusCode.OK;
}
catch (Exception ex)
{
return false;
}
}
}
private const string _rfc822DateFormat = " ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T ";
public static string FormatRfc822Date(DateTime dt)
{
return dt.ToUniversalTime().ToString(_rfc822DateFormat, CultureInfo.InvariantCulture);
}
string MD5Encrypt( string strText)
{
using ( var md5 = MD5.Create())
{
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(strText));
return BitConverter.ToString(result).Replace( " - ", "").ToUpper();
}
}
byte[] MD5EncryptByte( string strText)
{
using ( var md5 = MD5.Create())
{
return md5.ComputeHash(Encoding.UTF8.GetBytes(strText));
}
}
string Sign( string signatureString, string secretKey, bool isRaw = true)
{
var enc = Encoding.UTF8;
HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(secretKey));
hmac.Initialize();
byte[] buffer = enc.GetBytes(signatureString);
if (isRaw)
{
byte[] ret = hmac.ComputeHash(buffer);
return Convert.ToBase64String(ret);
}
else
{
string res = BitConverter.ToString(hmac.ComputeHash(buffer)).Replace( " - ", "").ToLower();
return Convert.ToBase64String(Encoding.UTF8.GetBytes(res));
}
}
/// <summary>
/// 外部DLL导入
/// </summary>
void ExportDll()
{
string path = AppDomain.CurrentDomain.BaseDirectory;
try
{
System.IO.DirectoryInfo DirInfo = new DirectoryInfo(path);
DirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
}
catch { }
#region zlib32.dll
string dllfile1 = Path.Combine(path, " zlib32.dll ");
if (!File.Exists(dllfile1))
{
File.WriteAllBytes(dllfile1, Resource.zlib32);
}
#endregion
#region zlib64.dll
string dllfile2 = Path.Combine(path, " zlib64.dll ");
if (!File.Exists(dllfile2))
{
File.WriteAllBytes(dllfile2, Resource.zlib64);
}
#endregion
}
class LogBody
{
public string logstoreName { get; set; }
public int ttl { get; set; }
public int shardCount { get; set; }
}
{
using ( var client = new HttpClient())
{
var time = DateTime.Now;
string body = Newtonsoft.Json.JsonConvert.SerializeObject( new LogBody() { logstoreName = logStore, ttl = 1, shardCount = 2 });
string date = FormatRfc822Date(time);
string contentMD5 = MD5Encrypt(body);
string signString = Sign( " POST\n " + contentMD5 + " \napplication/json\n " + date + " \nx-log-apiversion:0.6.0\nx-log-signaturemethod:hmac-sha1\n/logstores ", this.accessKeySecret);
HttpContent content = new StringContent(body);
string sign = string.Format( " {0}:{1} ", accessKeyId, signString);
client.DefaultRequestHeaders.Add( " x-log-apiversion ", " 0.6.0 ");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( " LOG ", sign);
client.DefaultRequestHeaders.Add( " host ", project + " . " + endpoint);
client.DefaultRequestHeaders.Add( " Date ", date);
client.DefaultRequestHeaders.Add( " x-log-signaturemethod ", " hmac-sha1 ");
client.DefaultRequestHeaders.Add( " ContentMD5 ", contentMD5);
content.Headers.ContentType = new MediaTypeHeaderValue( " application/json ");
try
{
var res = await client.PostAsync( string.Format( " http://{0}.{1}/logstores ", project, endpoint), content).ConfigureAwait( false);
return res != null && res.StatusCode == System.Net.HttpStatusCode.OK;
}
catch (Exception ex)
{
return false;
}
}
}
private const string _rfc822DateFormat = " ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T ";
public static string FormatRfc822Date(DateTime dt)
{
return dt.ToUniversalTime().ToString(_rfc822DateFormat, CultureInfo.InvariantCulture);
}
string MD5Encrypt( string strText)
{
using ( var md5 = MD5.Create())
{
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(strText));
return BitConverter.ToString(result).Replace( " - ", "").ToUpper();
}
}
byte[] MD5EncryptByte( string strText)
{
using ( var md5 = MD5.Create())
{
return md5.ComputeHash(Encoding.UTF8.GetBytes(strText));
}
}
string Sign( string signatureString, string secretKey, bool isRaw = true)
{
var enc = Encoding.UTF8;
HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(secretKey));
hmac.Initialize();
byte[] buffer = enc.GetBytes(signatureString);
if (isRaw)
{
byte[] ret = hmac.ComputeHash(buffer);
return Convert.ToBase64String(ret);
}
else
{
string res = BitConverter.ToString(hmac.ComputeHash(buffer)).Replace( " - ", "").ToLower();
return Convert.ToBase64String(Encoding.UTF8.GetBytes(res));
}
}
/// <summary>
/// 外部DLL导入
/// </summary>
void ExportDll()
{
string path = AppDomain.CurrentDomain.BaseDirectory;
try
{
System.IO.DirectoryInfo DirInfo = new DirectoryInfo(path);
DirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
}
catch { }
#region zlib32.dll
string dllfile1 = Path.Combine(path, " zlib32.dll ");
if (!File.Exists(dllfile1))
{
File.WriteAllBytes(dllfile1, Resource.zlib32);
}
#endregion
#region zlib64.dll
string dllfile2 = Path.Combine(path, " zlib64.dll ");
if (!File.Exists(dllfile2))
{
File.WriteAllBytes(dllfile2, Resource.zlib64);
}
#endregion
}
class LogBody
{
public string logstoreName { get; set; }
public int ttl { get; set; }
public int shardCount { get; set; }
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/p/6027164.html,如需转载请自行联系原作者