.NET 4.x Переделать код, чтобы POST-запросы отправлялись последовательно в 1 поток - C#
Формулировка задачи:
Есть пример многопоточной отправки пост запросов через проски Чарлеса, нужно переделать чтобы запросы отправлялись последовательно в 1 поток
namespace bot
{
public partial class Form1 : Form
{
readonly ParallelOptions o = new ParallelOptions();
private Thread t;
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
t = new Thread(s_w);
t.Start();
}
string send_POST(string url, string post)
{
string Response_POST = "", StrNewValue;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.Proxy = new WebProxy("127.0.0.1", 8888);
req.ContentType = "application/x-www-form-urlencoded";
StrNewValue = post;
req.ContentLength = StrNewValue.Length;
StreamWriter Soup = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
Soup.Write(StrNewValue);
Soup.Close();
StreamReader stin3 = new StreamReader(req.GetResponse().GetResponseStream());
Response_POST = stin3.ReadToEnd();
stin3.Close();
}
catch { }
return Response_POST;
}
private void s_w()
{
o.MaxDegreeOfParallelism = Convert.ToInt32(numericUpDown1.Value);
Parallel.ForEach(GetLine(), o,
line => send_POST("http://game-r01vk.rjgplay.com/command/", line));
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
IEnumerable<string> GetLine()
{
string[] line = null;
if (richTextBox1.InvokeRequired)
{
richTextBox1.Invoke(
new Action(() =>
{
line = richTextBox1.Text.Split('|');
}
));
}
else
line = richTextBox1.Text.Split('|');
return line;
}Решение задачи: «.NET 4.x Переделать код, чтобы POST-запросы отправлялись последовательно в 1 поток»
textual
Листинг программы
private void button1_Click_1(object sender, EventArgs e)
{
string text = this.send_POST("http://game-r01vk.rjgplay.com/command/", "<auth><auth_key>" + this.textBox2.Text + "</auth_key> <login>" + this.textBox1.Text + "</login></auth>");
this.label1.Text = this.parse(text, "<sid>", "</sid");
string text1 = this.send_POST("http://game-r01vk.rjgplay.com/command/", "<get_game_info sid=\"" + this.label1.Text + "\"/>");
this.label2.Text = this.parse(text1, "<leader", "</leader");
string text2 = this.send_POST("http://game-r01vk.rjgplay.com/command/", "<update sid=\"" + this.label1.Text + "\"/>");
}
string res;
private string parse(string text, string ind1, string ind2)
{
res = "";
string[] stringSeparators = new string[] { "\n" };
string[] result = text.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (string stroka in result)
{
if (stroka.IndexOf(ind1) != -1)
{
string[] Moneyy = stroka.ToString().Split((Convert.ToChar(">")));
res = Moneyy[1].Replace(ind2, "");
}
}
return res;
}