using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace TestGameProduct
{
internal class Program
{
private static readonly HttpClient httpc = new HttpClient();
static async Task Main(string[] args)
{
FormUrlEncodedContent content = null;
var productsDto = new ProductRequestDto();
var applicationCode = "52e7cf9abc";
var secretKey = "e560111000101";
var version = "V1";
string source = applicationCode + version + secretKey;
using (var md5Hash = MD5.Create())
{
var hash = GetMd5Hash(md5Hash, source);
productsDto.signature = hash;
productsDto.ApplicationCode = applicationCode;
productsDto.version = version;
}
//Convert request
var keyValues = productsDto.ToKeyValues();
content = new FormUrlEncodedContent(keyValues);
await RunWithoutUsingHttpClient(content);
}
private static async Task RunWithoutUsingHttpClient(FormUrlEncodedContent content)
{
httpc.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", "QklNOlFqSjQ1R1");
for (int x = 0; x < 5000; x++)
{
HttpResponseMessage message = await httpc.PostAsync("http://test.com/test/Product/", content);
Console.WriteLine(message.StatusCode);
}
Console.WriteLine("Connections Established");
Console.ReadLine();
}
public static string GetMd5Hash(HashAlgorithm md5Hash, string input)
{
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
}
public static class ObjectExtension
{
public static IDictionary<string, string> ToKeyValues(this object metaToken)
{
if (metaToken == null)
{
return null;
}
JToken token = metaToken as JToken;
if (token == null)
{
return ToKeyValues(JObject.FromObject(metaToken));
}
if (token.HasValues)
{
var contentData = new Dictionary<string, string>();
foreach (var child in token.Children().ToList())
{
var childContent = child.ToKeyValues();
if (childContent != null)
{
contentData = contentData.Concat(childContent).ToDictionary(k => k.Key, v => v.Value);
}
}
return contentData;
}
var jValue = token as JValue;
if (jValue?.Value == null)
{
return null;
}
var value = jValue?.Type == JTokenType.Date
? jValue?.ToString("o", CultureInfo.InvariantCulture)
: jValue?.ToString(CultureInfo.InvariantCulture);
return new Dictionary<string, string> { { token.Path, value } };
}
}
public class ProductRequestDto
{
public string version { get; set; }
public string signature { get; set; }
public string ApplicationCode { get; set; }
}
}