C O*_*C O 6 c# multithreading httpclient
因此,在尝试使用Java和HttpClient之后,我决定转换到C#以尝试降低内存使用率,同时提高速度.我一直在阅读会员提供的关于异步与多线程的大量文章.似乎多线程将是更好的方向.
我的程序将访问服务器并反复发送相同的请求,直到检索到200个代码.原因是因为在高峰时段交通非常高.这使得服务器很难到达并引发5xx错误.代码非常简单,我不想添加循环来帮助简化观众.
我觉得我正朝着正确的方向前进,但我想与社区联系,以摆脱任何不良习惯.再次感谢您的阅读.
主持人请注意:我要求检查我的代码是否存在差异,我很清楚多线程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}");
// Send a request asynchronously continue when complete
var result = client.PostAsync(new Uri(url), queryString).Result;
// Check for success or throw exception
string resultContent = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(resultContent);
}
public static void Run()
{
getSession ();
// Not yet implemented yet..
//doMore ();
}
public static void Main (string[] args)
{
Console.WriteLine ("Welcome!");
// Create the threads
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);
// Start the thread
Thread1.Start();
Thread2.Start();
}
}
}
Run Code Online (Sandbox Code Playgroud)
此外,我不确定它是否重要,但我在我的MacBook Pro上运行它并计划在我的BeagleBoard上运行它.
如果我是你,我会怎么做.我会尽可能地利用异步,因为它比使用线程更有效(你很可能不需要一直上下文切换,这是昂贵的).
class MainClass
{
public static HttpClient client = new HttpClient();
static string url = "http://www.website.com";
public static async Task getSessionAsync()
{
StringContent queryString = new StringContent("{json:here}");
// Send a request asynchronously continue when complete
using (HttpResponseMessage result = await client.PostAsync(url, queryString))
{
// Check for success or throw exception
string resultContent = await result.Content.ReadAsStringAsync();
Console.WriteLine(resultContent);
}
}
public static async Task RunAsync()
{
await getSessionAsync();
// Not yet implemented yet..
//doMore ();
}
public static void Main(string[] args)
{
Console.WriteLine("Welcome!");
const int parallelRequests = 5;
// Send the request X times in parallel
Task.WhenAll(Enumerable.Range(1, parallelRequests).Select(i => RunAsync())).GetAwaiter().GetResult();
// It would be better to do Task.WhenAny() in a while loop until one of the task succeeds
// We could add cancellation of other tasks once we get a successful response
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我同意@Damien_The_Unbeliever:如果服务器在高负载下出现问题,则不应添加不必要的负载(执行相同请求的X次)并导致服务器问题.理想情况下,您将修复服务器代码,但我可以理解它不是您的.
弄乱标头不是线程安全的。
例如,用新的 OAuth 访问令牌替换 OAuth 访问令牌不是线程安全的。现在使用 Xamarin 移动应用程序来面对这个问题。我有一份崩溃报告,其中一个线程正在修改标头,而另一个线程正在尝试请求。