So, after discussing with Java and HttpClient, I decided to switch to C # to try to reduce memory usage while increasing speed. I have read many articles submitted by members here regarding async vs multithreading. Multithreading seems to be the best direction.
My program will access the server and send the same requests again and again until the 200 code is retrieved. The reason is that during peak hours the traffic is very high. This makes the server VERY inaccessible and throws 5xx errors. The code is very bare, I did not want to add a loop yet to help with simplicity for the audience.
I feel like I'm heading in the right direction, but I would like to appeal to the community to break out of any bad habits. Thanks again for reading.
Note for moderators: I ask you to check your code for discrepancies, I well know that this is good for multi-threaded HttpClient.
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Threading;
namespace MF
{
class MainClass
{
public static HttpClient client = new HttpClient();
static string url = "http://www.website.com";
public static void getSession() {
StringContent queryString = new StringContent("{json:here}");
var result = client.PostAsync(new Uri(url), queryString).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(resultContent);
}
public static void Run()
{
getSession ();
}
public static void Main (string[] args)
{
Console.WriteLine ("Welcome!");
ThreadStart threadref1 = new ThreadStart(Run);
ThreadStart threadref2 = new ThreadStart(Run);
Console.WriteLine("In the main: Creating the threads...");
Thread Thread1 = new Thread(threadref1);
Thread Thread2 = new Thread(threadref1);
Thread1.Start();
Thread2.Start();
}
}
}
Also, I'm not sure if it matters, but I run it on my MacBook Pro and plan to run it on my BeagleBoard.
source
share