2016-07-26 2 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#"이있는 항목입니다 (해당하는 경우 판단 할 수 없습니다). 게다가 ** 좋은 ** 제목 (구체적인 내용을 제외하고) 구체적인 질문을 한 다음 갑자기 많은 코드를 게시하고 번역을 요청합니다 (** 나쁜 **, 아무도 이것을하지 않고 downvoted하게됩니다). – 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