2016-11-10 194 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