2017-08-26 220 views
-2

我正在學習如何建立WEB-API客戶端ASP.NET API客戶端

我已經創造了一些簡單的API:

[HttpGet] 
     public IHttpActionResult GetInfo() 
     { 
      return Ok("Its working!"); 
     } 
     [HttpPost] 
     public IHttpActionResult PostInfo(ClientDataDto dto) 
     { 

      try 
      { 
       someMethod(dto.IdKlienta, dto.Haslo, dto.IdZgloszenia, dto.HardwareInfo, dto.SoftwareInfo); 

       return Ok("sent"); 
      } 
      catch 
      { 
       return BadRequest(); 
      } 
     } 

現在我只是試圖調用get方法。 當我使用Fiddler與地址 本地主機:someport/API /客戶端2 其工作

但是當我嘗試通過客戶端,代碼如下做到這一點:

private static HttpClient client = new HttpClient(); 

     static void Main(string[] args) 
     { 
      #region TESTONLY 
      var debug = new XMLData(); 
      string HardwareInfoXML = debug.HardwareXML; 
      string SoftInfoXML = debug.SoftwareXML; 
      int id_zgloszenia = 20; 
      int idKlienta = 25; 
      //haslo = "202cb962ac59075b964b07152d234b70"; 
      #endregion 

      var data = new ClientDataDto() { HardwareInfo = HardwareInfoXML, SoftwareInfo = SoftInfoXML, IdKlienta = idKlienta, IdZgloszenia = id_zgloszenia }; 
      RunAsync(data); 

     } 





     private static async Task RunAsync(ClientDataDto data) 
     { 
      var stringContent = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); 

      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
      client.BaseAddress = new Uri(@"http://localhost:7774/api/client2/"); 
      var url = new Uri(@"http://localhost:7774/api/client2/"); 

      var res1 = await client.GetAsync(url); 
      var res = await client.PostAsync(url, stringContent); 
      res.EnsureSuccessStatusCode(); 

     } 

應用收盤在沒有任何信息

var res1 = await client.GetAsync(url); 

我已經檢查,看在調試例外的Windows所有異常,但它正試圖打電話GetAsync PostASync沒有工作過之後,剛剛閉幕。

這裏有什麼問題?

回答

0

我真的很抱歉,我發佈了simpe問題。理解是在RunAsync(data)上添加.Wait();

RunAsync(data).Wait();