2016-07-26 10 views
-3

私は完璧に実行されるphpの次のコードを持っています、問題はそのループの中で数回実行されてからゆっくりです。また、PHPでマルチスレッドを実装することは、別の問題です。しかし、私はC#でマルチスレッドを行う方法を知っているので、もし誰でもこの関数を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; 
    } 
+0

それは何を取得しても変化するかもしれエラーあなたの 'C#'コードとを表示するのが最善だろう何の応答にエラーメッセージが表示されますありませんあなたの質問。 – zulq

+0

代わりに、成功したHttpClientを使用することをお勧めします。あなたが試したことを示すことができますか?たぶん、正しい方向に向けることができます。 – Jite

+0

あなたが探しているものを検索しようとしましたか? [Here](http://stackoverflow.com/q/21255725/1997232)は、「curl c#」(それが適用可能かどうかは判断できません)というものです。また、タイトルに具体的な質問がある場合は**良い**(重複しない限り)具体的な質問をしてから突然コードの集まりをポストして翻訳してください(これは**悪い**、誰もこれを実行せず、あなたは下降するでしょう)。 – Sinatr

答えて

1
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... 
    } 
    } 
} 
+0

は魅力的に機能しました!どうもありがとう! – indago