Knuceles
Knuceles
CC#
Created by Knuceles on 10/16/2023 in #help
❔ Traffic out on asp.net
No description
31 replies
CC#
Created by Knuceles on 4/26/2023 in #help
❔ Multithreading an async function
I have an async function that I want to multithread with n number of threads, but I get convert error if I initialize the thread with new Thread(methodName) From my understanding, I need to use the .Wait() method every time I call the request method, because it has a Task object.
int threadNumber = 5;
Thread[] threads = new Thread[threadNumber];

for (int i = 0; i < threadNumber; i++)
{
threads[i] = new Thread(request);
threads[i].Start();
}

for (int i = 0; i < threadNumber; i++) threads[i].Join();
int threadNumber = 5;
Thread[] threads = new Thread[threadNumber];

for (int i = 0; i < threadNumber; i++)
{
threads[i] = new Thread(request);
threads[i].Start();
}

for (int i = 0; i < threadNumber; i++) threads[i].Join();
The method request():
static async Task request()
{
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(5);
var res = await client.GetAsync(URI);
}
static async Task request()
{
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(5);
var res = await client.GetAsync(URI);
}
111 replies
CC#
Created by Knuceles on 4/6/2023 in #help
✅ Google Analytics Cookies
So I learned that google analytic cookies (identified as _ga, _gid, AMP_Token for name pairs) are generated from client-side from analytics.js. So if I open a browser, and visit a site that uses google analytics, I could find those cookies easily. I want to try implementing a C# program that could obtain those cookies. If I were to send an HTTPWebrequest, and inspect cookies, I believe only the server-side generated cookies show. So how would I see client-side generated cookies?
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);

req.CookieContainer = new CookieContainer();
using (var response = (HttpWebResponse)req.GetResponse())
{
Console.WriteLine(response.Cookies);
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);

req.CookieContainer = new CookieContainer();
using (var response = (HttpWebResponse)req.GetResponse())
{
Console.WriteLine(response.Cookies);
}
6 replies
CC#
Created by Knuceles on 3/26/2023 in #help
✅ Ways to deserialize Json with dynamic model
I'm currently testing out a new API that I discovered, and I want to explore bunch of API functions and the response data. It would simply take too much time for me to define a Type class as the JSON responses are nested with dozens of arrays and objects, so is there a way for me to dynamically convert response data (HttpResponseMessage) into a C# object? Simply using <dynamic> wouldn't convert it into a C# object
JsonSerializer.Deserialize<type>(String);
JsonSerializer.Deserialize<type>(String);
And I only want to use the optimized native JSON libraries provided in .net 6 and 7, so I wouldn't want to rely on Newton, etc. I'm also aware that there's JSON object to C# class convertor, such as https://json2csharp.com/, but I solely want to find a way to dynamically convert without needing C# classes.
22 replies