C# HttpClient 동기화 사용 방법

3259 단어 코드
반환값 함수 없음
  private async void  getservertime()
        {
            string servertime = "0";//           ,       ref out   
            HttpClientHandler handler1 = new HttpClientHandler();
            handler1.AllowAutoRedirect = false;
            HttpClient httpClient1 = new HttpClient(handler1);
            httpClient1.DefaultRequestHeaders.Add("user-agent", UserAgent);
            if (InternetGetConnectedState(0, 0) == false)
            {
                //   
                servertime = "0";
                return ;
            }
             
            try
            {
                HttpResponseMessage  result  = await  httpClient1.GetAsync("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp");
               
                if (result.IsSuccessStatusCode)
                {
                    result.EnsureSuccessStatusCode();
                    string responseBodyAsText = await result.Content.ReadAsStringAsync();
                    JObject sertime = JObject.Parse(responseBodyAsText);                  
                    servertime=sertime["data"]["t"].ToString();                 
                }
                    
                else
                {
                    servertime= "0";
                }
               

            }
            catch (HttpRequestException hre)
            {
                servertime = "0"; ;
            }
            handler1.Dispose();
            httpClient1.Dispose();


        }

반환값 함수 있음
    private string getservertime()

        {
            HttpClientHandler handler1 = new HttpClientHandler();
           
            handler1.AllowAutoRedirect = false;
            HttpClient httpClient1 = new HttpClient(handler1);
            httpClient1.DefaultRequestHeaders.Add("user-agent", UserAgent);
            if (InternetGetConnectedState(0, 0) == false)
            {
              //   
                return "0";
            }
             
            try
            {
                var task = httpClient1.GetAsync("https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp");
                task.Wait();
                if (task.Result.IsSuccessStatusCode)
                    task.Result.EnsureSuccessStatusCode();
                else
                {
                    return "0";
                }
                HttpResponseMessage response = task.Result;

                //  txtStatusCode.Text = response.StatusCode + " " + response.ReasonPhrase + Environment.NewLine;

                var result = response.Content.ReadAsStringAsync();
                string responseBodyAsText = result.Result;

                JObject sertime = JObject.Parse(responseBodyAsText);
                handler1.Dispose();
                httpClient1.Dispose();
                return (sertime["data"]["t"].ToString());


            }
            catch (HttpRequestException hre)
            {
                hre.ToString();
            }
            handler1.Dispose();
            httpClient1.Dispose();
            return "0";

          
        }

좋은 웹페이지 즐겨찾기