C#中的旋度等价

本文关键字: | 更新日期: 2023-09-27 17:58:41

我在php中有以下代码运行得很好,问题是它运行得很慢,因为它在一个循环中运行了好几次。在php中实现多线程是另一个问题。但我知道如何在C#中执行多线程,所以如果有人能将这个函数转换为C#,我将处理多线程部分。

 function process_registration($reg,$phone,$key,$secret)
   {
    $fields['regno']= $reg; 
    $fields['phone']= $phone; 
    $fields['key']= $key;
    $fields['secret']= $secret;
    $process = curl_init('http://theip/registrationsearch/confirm_status.php'); 
    curl_setopt($process, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($process);
    $the_data=json_decode($result,true);
    if($the_data['Status']==='Ok') return $the_data['registration_details'];
    else return $the_data['Status'];
    }

我想在C#控制台应用程序中实现这一点。这样我就可以实现C#的多线程功能了。我尝试过使用HttpClient,但没有成功,有人能帮忙吗?

这是我在C#中做的功能,但我没有得到响应,没有错误消息

static async Task<int> MainAsync(string[] args)
    {
        var client = new HttpClient();
        var keyValues = new List<KeyValuePair<string, string>>();
        keyValues.Add(new KeyValuePair<string, string>("key", args[0]));
        keyValues.Add(new KeyValuePair<string, string>("secret", args[1]));
        keyValues.Add(new KeyValuePair<string, string>("phone", args[2]));
        keyValues.Add(new KeyValuePair<string, string>("regno", args[3]));
        var requestContent = new FormUrlEncodedContent(keyValues);
        HttpResponseMessage response = await client.PostAsync("http://theip/registrationsearch/confirm_status.php", requestContent);
        HttpContent responseContent = response.Content;
        Console.WriteLine("Waiting for response...");
        using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
        {
            Console.WriteLine(await reader.ReadToEndAsync());
        }
        return 1;
    }

C#中的旋度等价

private static async void TestAsyncPost()
{
  var values = new Dictionary<string, string>();
  values.Add("regno", "testReg");
  values.Add("phone", "testPhone");
  values.Add("key", "testKey");
  values.Add("secret", "testSecret");
  var content = new FormUrlEncodedContent(values);
  using (var client = new HttpClient())
  {
    try
    {
      var httpResponseMessage = await client.PostAsync("http://theip/registrationsearch/confirm_status.php", content);
      if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
      {
        // Do something...
        var response = await httpResponseMessage.Content.ReadAsStringAsync();
        Trace.Write(response);  // response here...
      }
    }
    catch (Exception ex)
    {
      Trace.Write(ex.ToString()); // error here...
    }
  }
}
相关文章:
  • 没有找到相关文章