2016-11-10 276 views
0

我寫的應用程序爲UWPPOST請求不發送

我收到來自後端JSON是這樣的:

string url = "http://api.simplegames.com.ua/index.php/?wc_orders=all_orders"; 
{ 
    string jsonString; 

    using (var httpClient = new System.Net.Http.HttpClient()) 
    { 
     var stream = await httpClient.GetStreamAsync(url); 
     StreamReader reader = new StreamReader(stream); 
     jsonString = reader.ReadToEnd(); 
    } 

    return jsonString; 
} 

我嘗試發送這樣

OrdersList = new List<RootObject>(rootObjectData); 
using (HttpClient httpClient = new HttpClient()) 
{ 
    httpClient.BaseAddress = new Uri(@"http://api.simplegames.com.ua"); 
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
    httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8")); 

    string endpoint = @"/post_from_local.php"; 

    try 
    { 
     HttpContent content = new StringContent(JsonConvert.SerializeObject(OrdersList), Encoding.UTF8, "application/json"); 
     HttpResponseMessage response = await httpClient.PostAsync(endpoint, content); 

     if (response.IsSuccessStatusCode) 
     { 
      string jsonResponse = await response.Content.ReadAsStringAsync(); 
      Debug.WriteLine(jsonResponse); 
      //do something with json response here 
     } 
    } 
    catch (Exception) 
    { 
     //Could not connect to server 
     //Use more specific exception handling, this is just an example 
    } 
} 

但是POST請求,後端dev表示他看到空行,但數據沒有收到。

感謝您的幫助。我的錯誤在哪裏?

回答

1

改變這一行:

var stream = await httpClient.GetStreamAsync(url); 

到:

var stream = httpClient.GetStreamAsync(url).Result; 
+0

不工作。我發現我明白我的問題在哪裏。我在異步中的問題在等待。我試圖在發送json變量之前通過發送請求發送json – Eugene

+0

正是這樣,這就是爲什麼我認爲這可以解決問題。 – Mahdi

+0

好的。我像你說的那樣重寫。但它似乎沒有解決問題 – Eugene