-3

i have the following code in php that runs perfect, the problem is that its slow, since its run several times in a loop. And implementing multithreading in php is another problem all together. But i know how to do multithreading in C#, so if anyone can convert this function to a C# i will handle the multithreading part.

 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'];
    }

I want to implement this in a C# console application. So that i can implement C#'s Multithreading capabilities. I have tried using HttpClient with no success, can anyone help?

This is the function i did in C# but i get no response no error message

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;
    }
4
  • It would be best to show your C# code and what errors you getting and may be also change your question. Commented Jul 26, 2016 at 10:03
  • I would recommend trying to use the HttpClient with success instead. Could you show what you have tried? Maybe then we can point you in the right direction. Commented Jul 26, 2016 at 10:03
  • Have you tried to search for what you are asking? Here is something with "curl c#" (I can't judge if it's applicable). Moreover you ask good (unless duplicate) concrete question in title and then suddenly post a bunch of code and asking to translate it (this is bad, nobody will do this and you'll get downvoted). Commented Jul 26, 2016 at 10:03
  • It might also be a good idea to consider making your code a bit more optimal than it already is instead of starting to mix in threads. I don't think the number of threads is the real issue if the script is slow. Commented Jul 26, 2016 at 10:04

1 Answer 1

2
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...
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.